Take value from query to label in C#

前端 未结 8 1135
逝去的感伤
逝去的感伤 2021-01-26 12:48

How can i take value from query result to label?

I have two label, one is labelName and one more is labelDepartment

So when i r

8条回答
  •  醉话见心
    2021-01-26 13:12

    One way to do:

    private void getData()
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand(" SELECT tbl_staff.staffName,tbl_department.department 
    FROM tbl_staff,tbl_logs,tbl_department 
    WHERE tbl_staff.userID = tbl_logs." + listStaff.SelectedValue + " and tbl_staff.idDepartment = tbl_department.idDepartment", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
    
        sqlCmd.Parameters.AddWithValue("@username",user);
        sqlDa.Fill(dt);
        if (dt.Rows.Count > 0)
        {
               lable.Text = dt.Rows[0]["staffName"].ToString(); //Where "staffName" is ColumnName 
    
        }
            connection.Close();
    }
    

    Suggest to use a stored procedure not in line query to avoid SQL injection attacks.

    stored procedure example

提交回复
热议问题