SqlHelp文件
#region 创建数据库连接
private static SqlConnection conn = new SqlConnection(@"Data Source=RAD\TEST;Initial Catalog=JDIERP;Persist Security Info=True;User ID=sa;Password=123456");//创建连接
private static SqlDataAdapter da = new SqlDataAdapter();
private static DataSet ds = new DataSet();
private static DataTable dt = new DataTable();
private static SqlCommand comm = new SqlCommand();
#endregion
#region 打开连接
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
}
#endregion
#region 关闭连接
private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
#endregion
#region sql语句返回一个数据集DataSet
public static DataSet dataSet(string sSql)
{
try
{
openConnection();
da = new SqlDataAdapter(sSql, conn);
ds.Clear();//清空原来的数据
da.Fill(ds);//填充数据到ds中
closeConnection();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
//finally
//{
// closeConnection();
//}
return ds;
}
#endregion
#region 执行一条sql的增insert、删delete、改update语句,返回一个布尔值
public static bool ExecuteNonQuery(string sSql)
{
bool b;
try
{
openConnection();
comm = new SqlCommand(sSql, conn);
if (comm.ExecuteNonQuery() > 0)
{
b = true;
}
else
{
b = false;
}
closeConnection();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
//finally
//{
// closeConnection();
//}
return b;
}
#endregion
#region 执行一条sql的查询语句,返回一个字段的值
public static string ExecuteSqlValue(string table, string s1, string value, string s2)
{
string s = "";
try
{
openConnection();
comm = new SqlCommand("select top 1 " + s2 + " from " + table + " where " + s1 + " =" + value, conn);
s = comm.ExecuteScalar().ToString();
closeConnection();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
//finally
//{
// closeConnection();
//}
return s;
}
#endregion
#region 执行一条sql语句
public static void ExecuteSql(string sqlStr)
{
try
{
openConnection();
comm = new SqlCommand(sqlStr, conn);
closeConnection();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
//finally
//{
// closeConnection();
//}
}
#endregion
//弹出消息框
public static void MsgBox(string msg)
{
System.Web.HttpContext.Current.Response.Write("<script>alert('" + msg + "')</script>");
}

浙公网安备 33010602011771号