【问题标题】:how to get data from sql one by one row如何从sql中逐行获取数据
【发布时间】:2018-05-29 05:53:23
【问题描述】:

我会给项目的条形码,然后项目数据将被提取到 datagridview 中。

这些代码只获取一行,如果我再次填写条形码,则此代码更新行不更新新行。我想换新行

SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=QuickBook;Integrated Security=True");
string query = "select productName,productDescription,productUnitPrice from productsInfo where productBarcode =" + p_barcode.Text + "";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataTable dt = new DataTable();

da.Fill(dt);
dataGridView1.DataSource = dt;

【问题讨论】:

  • 得到新结果后是否重新绑定datagridview?另外,您的 SQL 语句是否在文本框值周围使用撇号限定?

标签: c# sql datagrid


【解决方案1】:

尝试用'撇号检查

string query = "select productName,productDescription,productUnitPrice from productsInfo where productBarcode = '" + p_barcode.Text + "'";

【讨论】:

    【解决方案2】:
      da.Fill(dt); 
    
    //this loop is pseudocode. Dont remember the exakt syntax
        Foreach(DataRow r in dt) 
        {
            dgvDT. Rows. Add(r. Copy()) 
        } 
    
        dataGridView1.DataSource = dgvDT;
    

    使 dgvDT 成为数据成员而不是局部变量

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 2017-05-05
      • 2016-03-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 2015-08-09
      • 2014-11-12
      • 2021-08-24
      相关资源
      最近更新 更多