添加引用:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
连接字符串:
private static string strConn = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
SQL查询:
public static DataTable Query(string sql)
{
DataTable DT = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter(sql,strConn);
SDA.Fill(DT);
return DT;
}
SQL非查询:
public static int NonQuery(string sql)
{
int num = 0;
SqlConnection conn = new SqlConnection(strConn);
SqlCommand CMD = new SqlCommand(sql, conn);
try
{
conn.Open();
num = CMD.ExecuteNonQuery();
}
finally
{
conn.Close();
}
return num;
}