winform中执行sql语句方法
发布网友
发布时间:2023-03-05 01:14
我来回答
共1个回答
热心网友
时间:2024-11-24 00:25
public int GetSingle(string sql)
{
object obj = DbHelperSQL.GetSingle(sql);
if (Object.Equals(obj, null))
{
return 0;
}
else
return int.Parse(obj.ToString());
}
public static object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
用第一个方法调用第二个,参数为查询sql语句