ASP.NET入门 - SqlHelper类
2023-12-19 01:50:37    74    0    0
admin

/// <summary>
/// 获取数据库连接
/// </summary>
/// <returns></returns>
public static SqlConnection getSqlConnection()
{
//string strcon = "Data Source=192.168.1.120;Initial Catalog=weight;";
string strcon = ConfigurationManager.ConnectionStrings["DBConnect"].ConnectionString; ;
return new SqlConnection(strcon);
}
/// <summary>
/// 查询数据
/// </summary>
/// <param name="strsql"> 查询的SQL语句</param>
/// <returns>表格数据</returns>
public static DataTable GetDataSet(string strsql)
{
//string strcon = "Data Source=localhost;Initial Catalog=weight;Integrated Security=TRUE";
SqlConnection con = getSqlConnection();
DataSet ds = new DataSet();
try
{
SqlDataAdapter DA = new SqlDataAdapter(strsql, con);
DA.Fill(ds, "tb");
}
catch (SqlException E)
{
}
con.Close();//关闭数据库
return ds.Tables[0];
}
/// <summary>
/// 执行SQL 增删改
/// </summary>
/// <param name="strsql">执行</param>
/// <returns></returns>
public static bool ExecuteSql(string strsql)
{
SqlCommand cmd = null;
using (cmd = new SqlCommand())
{
try
{
cmd.CommandText = strsql;//设置SQL语句
cmd.Connection = getSqlConnection();//调用打开数据库连接方法
cmd.Connection.Open();
cmd.ExecuteNonQuery();//执行
return true;
}
catch (Exception e)
{
return false;
}
}
}​

Pre: ASP.NET入门 - 连接数据库

Next: ASP.NET入门 - JsHelper类

74

ICP备案号: 赣ICP备2020012703号-1

表格内容