private static string CONN = @"Data
Source=\SQLEXPRESS; DataBase=tb_taizhan;Integrated
Security=true";
执行查询
dataset
public static DataSet ExecuteSelectSql(string strsql)
{
SqlConnection sqlconn = new SqlConnection(CONN);
SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlconn);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw new Exception("执行SQL语句出错:\r\n" + strsql + "\r\n" +
ex.ToString());
}
finally
{
sqlconn.Close();
}
}
///
/// 执行更新
///
///
///
int
public static int ExecuteUpdateSql(string strsql)
{
SqlConnection sqlconn = new SqlConnection(CONN);
SqlCommand cmd = new SqlCommand(strsql);
cmd.Connection = sqlconn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strsql;
int rec = -1;
try
{
sqlconn.Open();
rec = cmd.ExecuteNonQuery();
return rec;
}
catch (Exception ex)
{
throw new Exception("执行SQL语句出错:\r\n" + strsql + "\r\n" +
ex.ToString());
}
finally
{
sqlconn.Close();
}
}
///
/// 执行删除
///
///
///
int
public static int ExecuteDelSql(string strsql)
{
SqlConnection sqlconn = new SqlConnection(CONN);
SqlCommand cmd = new SqlCommand(strsql, sqlconn);
int rec = -1;
try
{
sqlconn.Open();
rec = cmd.ExecuteNonQuery();
return rec;
}
catch (Exception ex)
{
throw new Exception("执行SQL语句出错:\r\n" + strsql + "\r\n" +
ex.ToString());
}
finally
{
sqlconn.Close();
}
}
///
/// 执行Insert语句
///
///
///
public static int ExecuteInsertSql(string strSql)
{
SqlConnection conn = new SqlConnection(CONN);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = strSql;
int ret = -1;
try
{
conn.Open();
ret = comm.ExecuteNonQuery();
return ret;
}
catch (Exception ex)
{
throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" +
ex.ToString());
}
finally
{
conn.Close();
}
//throw new NotImplementedException();
}
public static Object ExecuteScalar(string strSql)
{
SqlConnection conn = new SqlConnection(CONN);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = strSql;
try
{
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
return dr[0];
}
else
{
return null;
}
}
catch (Exception ex)
{
throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" +
ex.ToString());
}
finally
{
conn.Close();
}
//throw new NotImplementedException();
}