日期:2014-05-18  浏览次数:20844 次

谁能告诉我这代码错在哪了?关于ACCESS某一个字段显示在textbox上
string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\db1.mdb;";
  OleDbConnection conn = new OleDbConnection(strconn);
  conn.Open();
  string sql = "select [Time] from [project] where [Name]='" + DropDownList1.SelectedValue + "'";
  SqlDataReader reader = new SqlDataReader(sql, conn);
  if (reader.Read())
  {
  TextBox3.Text = reader[0].ToString();
  }

提示“System.Data.SqlClient.SqlDataReader”不包含采用“2”参数的构造函数 的错误,请问哪里出问题了,应该怎么改?

------解决方案--------------------
SqlDataReader类没有2个参数的构造函数,SqlDataReader reader = new SqlDataReader(sql, conn);改为 SqlDataReader reader = new SqlDataReader(conn);

------解决方案--------------------
MSDN

C# code
private static void ReadOrderData(string connectionString)
{
    string queryString =
        "SELECT OrderID, CustomerID FROM dbo.Orders;";

    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        // Call Read before accessing data.
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }

        // Call Close when done reading.
        reader.Close();
    }
}

------解决方案--------------------
SqlDataReader reader = new SqlDataReader(sql, conn);
改成
OleDbCommand cmd=new OleDbCommand(sql.conn);
OleDbDataReader reader=cmd.ExecuteReader();
------解决方案--------------------
string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\db1.mdb;";
OleDbConnection conn = new OleDbConnection(strconn);
conn.Open();
string sql = "select [Time] from [project] where [Name]='" + DropDownList1.SelectedValue + "'";
OleDbCommand cmd=new OleDbCommand(sql.conn);
OleDbDataReader reader=cmd.ExecuteReader();
if (reader.Read())
{
TextBox3.Text = reader[0].ToString();
}
conn.Close();