这是写在buttonclick里面的,运行到数据库链接那块就发生错误,而且读不到数据
string area = this.comboBox1.SelectedValue.ToString();               
                string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";
                strConnection += @"Data Source=|DataDirectory|db1.mdb";
                //获取数据库表进行查询
                 OleDbConnection objConnection = new OleDbConnection(strConnection);  //建立连接
                objConnection.Open();  //打开连接
                OleDbCommand sqlcmd = new OleDbCommand(@"select WEIGHT_F,WEIGHT_S from TB_02 where AREA = area and MAIL = '+韵达+'", objConnection);  //sql语句
                OleDbDataReader reader = sqlcmd.ExecuteReader();   //执行查询
                float weight_f = new float();
                float weight_s = new float();
                if (reader.Read())
                { //这个read调用很重要!不写的话运行时将提示找不到数据
                    weight_f = (float)reader["WEIGHT_F"];  //取得字段的值
                    weight_s = (float)reader["WEIGHT_S"];
                    objConnection.Close();
                    reader.Close();                    float total02;
                    if (float.Parse(tb) <= 1) { total02 = weight_f; }
                    else if (1 < float.Parse(tb) && float.Parse(tb) <= 4) { total02 = weight_f + 1; }
                    else { total02 = weight_f + 1 + (float.Parse(tb) - 4) * weight_s; }
                    textBox2.Text = Convert.ToString(total02);
                }
                else { MessageBox.Show("获取数据错误"); textBox2.Text = ""; }

解决方案 »

  1.   

    objConnection.Open(); //打开连接是上面这一句出错吗?修改一下连接字符串:
    string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";
      strConnection += @"Data Source=|DataDirectory|db1.mdb";修改为
    string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";
      strConnection += @"Data Source=|DataDirectory|\db1.mdb";
      

  2.   

     OleDbCommand sqlcmd = new OleDbCommand(@"select WEIGHT_F,WEIGHT_S from TB_02 where AREA = area and MAIL = '+韵达+'", objConnection); //sql语句
    肯定是你的SQl语句
    你用参数吧
    string sql="select select WEIGHT_F,WEIGHT_S from TB_02 where AREA = area and MAIL = @MAIL"
    SqlParameter ps = new SqlParameter("@MAIL",你的值);
      

  3.   

    谢谢大家,可以都试了现在出现了 weight_f = (float)reader["WEIGHT_F"];  //取得字段的值
    这个提示是:
      

  4.   

    "select WEIGHT_F,WEIGHT_S from TB_02 where AREA = '"+area+"' and MAIL = '韵达'"
    结贴