在C#中从查询到标签获取值

时间:2012-09-03 06:44:37

标签: c# sql

如何从查询结果中获取值到标签?

我有两个标签,一个是 labelName ,还有一个是 labelDepartment

因此,当我运行查询时,如何从查询结果中获取值并使用c#将其分配给标签?

这是我的Sql命令:

  "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;"

这是C#中的当前代码

//Open SQL connection

SqlConnection openCon = new SqlConnection(connString);
openCon.Open();

string SQL = string.Format("SELECT tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs.userID and tbl_staff.idDepartment = tbl_department.idDepartment" + listStaff.SelectedValue + ";");


SqlCommand command = new SqlCommand(SQL);
SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
    labelName.Text = reader["tbl_staff.staffName"];
    labelDepartment.Text = reader["tbl_department.department"];
}

注意:我们的记录只会返回一行.. MS SQL和C#..感谢您的帮助......;)

8 个答案:

答案 0 :(得分:1)

您需要通过SQLDataReader

读取结果
SQLCommand command = new SQLCommand("your sql string here");
SQLDataReader reader = command.executeReader();
while(reader.read())
{
 set your label values here with reader["cloumn"]
}

答案 1 :(得分:1)

        string name = null;
        string department = null;
        string listStaff = "MylistStaff";

        string sql =  "SELECT tbl_staff.staffName,tbl_department.department " +
            "FROM tbl_staff,tbl_logs,tbl_department " +
            "WHERE tbl_staff.userID = tbl_logs." + listStaff + " and tbl_staff.idDepartment = tbl_department.idDepartment;";
        //change this connection string... visit www.connectionstrings.com
        string connString = "Server=localhost; Database=myDatabaseName; Trusted_Connection=Yes";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand command = new SqlCommand(sql,conn))
            {
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    name = reader[0] as string;
                    department = reader[1] as string;
                    //break for single row or you can continue if you have multiple rows...
                    break;
                }
            }
            conn.Close();
        }

department和listStaff可以轻松应用于标签文本,如:

DepartmentLabel.Text = department;

答案 2 :(得分:0)

您可以阅读使用DataReader类返回的查询行的内容。它有获取单个值的方法,或者您可以迭代每一行。告诉我你查询返回了多少行,这样我就可以提供确切的代码。

答案 3 :(得分:0)

选中此项 - 同时将连接字符串与此代码一起设置

SQLCommand command = new SQLCommand();
command.CommandText = " 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 ";

SQLDataReader reader = command.executeReader();

while(reader.read())
{
 labelName.Text = reader.GetString(0);
 labelDepartment.Text = reader.GetString(1);
}

答案 4 :(得分:0)

一种方法:

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();
}

建议使用不在行查询中的存储过程来避免SQL注入攻击。

stored procedure example

答案 5 :(得分:0)

//Open SQL connection

SqlConnection openCon = new SqlConnection(connString);
openCon.Open();

string SQL = string.Format("SELECT tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs.userID and tbl_staff.idDepartment = tbl_department.idDepartment" + listStaff.SelectedValue + ";");


SqlCommand command = new SqlCommand(SQL);
SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
    labelName.Text = reader["tbl_staff.staffName"].ToString();
    labelDepartment.Text = reader["tbl_department.department"].ToString();
}

转换类型就是您在代码中留下的内容....

答案 6 :(得分:0)

尝试仅更改此

SqlConnection openCon = new SqlConnection(connString);
openCon.Open();

string SQL = string.Format("SELECT tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs.userID and tbl_staff.idDepartment = tbl_department.idDepartment" + listStaff.SelectedValue + ";");


SqlCommand command = new SqlCommand(SQL);
SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
    labelName.Text = reader["tbl_staff.staffName"].toString();
    labelDepartment.Text = reader["tbl_department.department"].toString();
}`

答案 7 :(得分:-2)

使用您的查询填充数据集,然后从数据集中检索值并将其分配给您的标签。

让我们说你的数据集是DS:

labelName.Text=DS.Tables[0]["tbl_staff.staffName"].tostring();
labelDepartment.Text=DS.Tables[0]["tbl_department.department"].tostring();

希望这有帮助。