日期:2014-05-17  浏览次数:20835 次

怎么将SQl中查询的一行一列的值传送到C#中的变量
SQL语句"select wname from tablename where wno="xxx"";

------解决方案--------------------
ADO.NET

C# code

string retVal = "";
string sql = "select wname from tablename where wno='xxx'";

using (SqlConnection conn = new SqlConnection(connectionString))
{
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = strSql;
    cmd.CommandType = CommandType.Text;
    cmd.Connection = conn;
    conn.Open();
    retVal = cmd.ExecuteScalar().ToString();
    conn.Close();
}
return retVal;