转载:https://blog.csdn.net/u011555996/article/details/78979401?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159668618819725211923116%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=159668618819725211923116&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v3~pc_rank_v2-1-78979401.first_rank_ecpm_v3_pc_rank_v2&utm_term=c%E8%BF%9E%E6%8E%A5sqlserver%E6%95%B0%E6%8D%AE%E5%BA%93&spm=1018.2118.3001.4187
// 数据库操作类
class Express
{
public string connString = "Data Source=xp;Initial Catalog=ExpressManager;Integrated Security=TRUE";
//创建连接对象的变量
public SqlConnection conn;
// 执行对数据表中数据的增加、删除、修改操作
public int NonQuery(string sql)
{
conn = new SqlConnection(connString);
int a = -1;
try
{
conn.Open(); //打开数据库
SqlCommand cmd = new SqlCommand(sql, conn);
a = cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close(); //关闭数据库
}
}
return a;
}
// 执行对数据表中数据的查询操作
public DataSet Query(string sql)
{
conn = new SqlConnection(connString);
DataSet ds = new DataSet();
try
{
conn.Open(); //打开数据库
SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
adp.Fill(ds);
}
catch
{
}
finally
{
if(conn.State== ConnectionState.Open)
conn.Close(); //关闭数据库
}
return ds;
}
}