Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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# 如何从sql数据库表将数据加载到文本框中_C#_Mysql_Sql Server_Winforms - Fatal编程技术网

C# 如何从sql数据库表将数据加载到文本框中

C# 如何从sql数据库表将数据加载到文本框中,c#,mysql,sql-server,winforms,C#,Mysql,Sql Server,Winforms,我有个问题,我无法将数据加载到文本框中,该文本框是查询以windows窗体从数据库获取的。while循环无法执行。如何解决这个问题。或者没有任何错误或异常。内部命令无法执行调试器移动以捕获和完成 private void btnCheck_Click(object sender, EventArgs e) { try { // query = "SELECT Id, Emplname, CNIC, City, MobileNo,

我有个问题,我无法将数据加载到文本框中,该文本框是查询以windows窗体从数据库获取的。while循环无法执行。如何解决这个问题。或者没有任何错误或异常。内部命令无法执行调试器移动以捕获和完成

private void btnCheck_Click(object sender, EventArgs e)
    {
        try
        {
             //  query = "SELECT Id, Emplname, CNIC, City, MobileNo, Address, Salary, DailyWage, CompanyId, Status FROM Employees where id = '" + labCompyId.Text + "'";
        query = "SELECT CNIC, City, MobileNo, Address, Salary, DailyWage, Status FROM  Employees WHERE (EmployId = '"+txtEmployId+"') AND (Emplname = '"+txtEmplyName+"')";
        SqlCommand  command1 = DBConnectivity.getCommandForQuery(query, connection);
        SqlDataReader reader1 = command1.ExecuteReader();


        while(reader1.Read())
       {
        this.txtCNIC.Text = (reader1["CNIC"].ToString());
        this.txtEmplyCity.Text = (reader1["City"].ToString());
        this.txtEmplyAddress.Text = (reader1["Address"].ToString());
        this.txtSalary.Text = (reader1["Salary"].ToString());
        this.txtDailyWage.Text = (reader1["DailyWage"].ToString());

            reader1.Close();
        }


        }
        catch (Exception ex)
        {

        }

    }

哦,什么,停下来!!!使用参数化查询避免SQL注入

连接中提及您的连接字符串

我希望问题是您在select查询中遗漏了SD
txtEmployId.Text
值和
txtEmplyName.Text

SqlConnection  connection= new SqlConnection(your Connection string);
string query = "SELECT CNIC, City, MobileNo, Address, Salary, DailyWage, Status   
       FROM  Employees WHERE EmployId =@EmpID AND Emplname = @Emplname ";
SqlCommand  command1 = new SqlCommand(query, connection); 
connection.Open();
command1.Parameters.AddWithValue("@EmpID",txtEmployId.Text);
command1.Parameters.AddWithValue("@Emplname",txtEmplyName.Text);
SqlDataReader reader1 = command1.ExecuteReader();


    while(reader1.Read())
   {
    this.txtCNIC.Text = (reader1["CNIC"].ToString());
    this.txtEmplyCity.Text = (reader1["City"].ToString());
    this.txtEmplyAddress.Text = (reader1["Address"].ToString());
    this.txtSalary.Text = (reader1["Salary"].ToString());
    this.txtDailyWage.Text = (reader1["DailyWage"].ToString());

        reader1.Close();
    }
  • 你的连接字符串在哪里?如果不在页面中,请包括一个
  • 像Con.Open()一样打开连接
  • 使用参数化查询可以避免sql注入,但这只是一个建议

  • 在我看来,代码的问题在于连接字符串。请在try块中打开连接。

    catch
    块中您到底遇到了什么异常?