asp.net C# DBHelper类封装
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class DBHelper
{
//创建链接字符串
public static string sqlconn = "Data Source=.;Initial Catalog=StudentDB;Integrated Security=True";
//创建链接对象
public static SqlConnection conn = new SqlConnection(sqlconn);
//增删改方法
public static bool ExeQuteNonQuery(string sql)
{
//打开链接对象
conn.Open();
//创建命令对象
SqlCommand cmd = new SqlCommand(sql,conn);
//执行命令对象
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result > 0)
{
return true;
}
else {
return false;
}
}
//查询方法
public DataTable ExeQuteTable(string sql)
{
//创建数据适配器
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataTable table=new DataTable ();
da.Fill(table);
return table;
}
}
}