Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在文本框中获取数据_C#_Sql_Textbox - Fatal编程技术网

C# 在文本框中获取数据

C# 在文本框中获取数据,c#,sql,textbox,C#,Sql,Textbox,“我想做”当我在textbox上写下“项目代码”并单击搜索时,它将在sql数据库中搜索并找到具有该项目代码的项目。并将其名称带到textbox2(项目名称)。我如何做?我可以将其带到column/raw上,但Idk如何将该项目名称带到textbox2上 我可以搜索数据库中的数据并将其带到dataGridview if (textBox1.Text.Trim() == "") errorProvider1.SetError(textBox1, "Fiil in the blanks"); els

“我想做”当我在textbox上写下“项目代码”并单击搜索时,它将在sql数据库中搜索并找到具有该项目代码的项目。并将其名称带到textbox2(项目名称)。我如何做?我可以将其带到column/raw上,但Idk如何将该项目名称带到textbox2上

我可以搜索数据库中的数据并将其带到dataGridview

if (textBox1.Text.Trim() == "") errorProvider1.SetError(textBox1, "Fiil in the blanks");
else errorProvider1.SetError(textBox1, "");

conn.Open();
string regs = "SELECT * from stackTable where itemCodu=@Cod";

SqlCommand cmd = new SqlCommand(regs, conn);
cmd.Parameters.AddWithValue("@cod", textBox1.Text);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gridView.DataSource = dt;
conn.Close();
---操作顺序(我想做:D)

  • 数据库检索

  • 如果找到,将数据带到Textbox2

  • 如果未找到,则返回错误提供程序、消息文本框


  • 因此,您有一个datatable,您所要做的就是用一个简单的if语句检查填充后的计数:

    if(dt.rows.count == 0)
    {
        MessageBox.Show("Not Found!!");
    }
    else
    {  
        textBox2.DataBindings.Add("Text", dt, "the column you want to use for textbox2");
    }
    
    您还应该使用
    语句,以便正确处理所有对象

    下面是一个我将如何编写您所拥有内容的示例:

    textbox2.DataBinding.Clear(); //clear the databinding so it can be added for the new data found.
    
    using(SqlConnection conn = new SqlConnection("Your connection string here")
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT * from stackTable where itemCodu=@Cod")
    {
         da.SelectCommand.Parameters.Add("@Cod", sqlDbType.VarChar).Value = textBox1.Text; //see comment below for this change
         da.fill(dt);
         if (dt.Rows.Count == 0)
         {
             //display not found message
         }
         else
         {
             //bind to textbox (described above)
         }
     }
    
    另外请注意,与其允许隐式类型化参数,不如指定参数应接收的数据类型。这可以通过使用Add而不是AddWithValue来完成

    cmd.Parameters.Add("@cod", SqlType.VarChar).Value = textBox1.Text;
    
    代码中最后要注意的是,您可以在不使用sqlDataAdapter命令的情况下使用sqlDataAdapter

    我还想补充一点:

    如果您知道从搜索中只会收到1或0行,并且您对绑定数据不感兴趣,那么只需将文本框的
    .Text
    设置为datatable中该行的列即可

    例如,如果希望在表中显示名为Item的列,只需使用

    textBox2.Text = dt.Rows[0]["Item"].ToString();
    

    您有点不清楚,是否不想使用dataGridView?您可以将文本框绑定到数据,并检查dt是否有行。count>0感谢您的评论:)dataGridView只是举个例子,我想对文本框执行数据库操作。“”检查dt是否有行。count>0”“这部分没问题,但是Idk,我怎么做textbox.Text(“来自数据库的信息”)……我不理解很多部分,但你花了时间,你应该得到绿色的记号。”。谢谢我的朋友。明天早上我会试着再读一遍。