C# and SqlDataAdapter

本文关键字:SqlDataAdapter and | 更新日期: 2023-09-27 18:33:22

 SqlConnection connect = new SqlConnection("Data Source=m**-PC; Initial Catalog=Mydb; Integrated Security=True");
 DataSet set = new DataSet();
 SqlDataAdapter data = new SqlDataAdapter();
 connect .Open();
 data.InsertCommand = new SqlCommand("SELECT Yazi FROM Ozgecmis", connect);
 data.Fill(set, "Yazi");
 connect.Close();
 int sayi = set.Tables["Yazi"].Columns.Count;
 TextBox1.Text = Convert.ToString(sayi);

我会将数据提取数据集并添加表,这样我就会学习计算列表并编写 texbox1

我不拿萨伊,但我没有错误。有什么问题?

C# and SqlDataAdapter

将代码更改为

    SqlConnection connect = new SqlConnection("Data Source=m**-PC; Initial Catalog=Mydb; Integrated Security=True");
    DataSet set = new DataSet();
    SqlDataAdapter data = new SqlDataAdapter("SELECT Yazi FROM Ozgecmis", connect);
    data.Fill(set, "Yazi");
    int sayi = set.Tables["Yazi"].Columns.Count;
    TextBox1.Text = Convert.ToString(sayi);

您必须填写数据适配器的 select 命令,而不是插入命令,才能从数据库读取到数据集。这可以通过data.SelectCommand或在构造函数new SqlDataAdapter(...)中完成,如上面的代码所示。此外,连接的打开和关闭由数据适配器自动完成。

尝试:

    SqlDataAdapter data = new SqlDataAdapter("SQL QUERY HERE");
    ...
    TextBox1.Text = set.Tables[0].Columns.Count.ToString();

如果它不起作用,请写一条错误消息?