c#连接及查询sqlserver数据库,并取出相应字段的值的方法
ConnectionString为配置文件中配置的数据库连接串
//获取返回短信
public bool GetReturnSMS(string userNO)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT top 1 UserData FROM SGIPSend where DestAddr='" + userNO + "' order by createtime desc";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
this.returnSMS = reader.GetString(0);
Console.WriteLine("接收短信:" + this.returnSMS);
return true;
}
else
{
Console.WriteLine("接收短信:未获取到返回短信");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine("接收短信:数据库查询失败");
return false;
}
}