/// <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;
}
}
}