1.ADO.NET对象的使用
public class SQLHelper
{
#region Connectionstring 字符串
public static string Connectionstr = System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
#endregion
#region 返回值为int类型的执行ExecuteScalar
public static int ExecuteScalar(string sql)
{
//返回的值是int
SqlConnection con = new SqlConnection(Connectionstr);
SqlCommand com = new SqlCommand(sql, con);
con.Open();
int i = (int)com.ExecuteScalar();
con.Close();
return i;
}
#endregion
#region 返回值为object 类型的参数化执行ExecuteScalar
public static object ExecuteScalar(string sql, params SqlParameter[] pms)
{
//返回的值是int
SqlConnection con = new SqlConnection(Connectionstr);
SqlCommand com = new SqlCommand(sql, con);
if (pms != null)
{
com.Parameters.AddRange(pms);
}
con.Open();
object i = com.ExecuteScalar();
con.Close();
return i;
}
#endregion
#region 返回值为DataTable的 ExecuteDataTable
public static DataTable ExecuteDataTable(string sql)
{
//返回的值是一个表
SqlConnection con = new SqlConnection(Connectionstr);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
con.Open();
DataTable dat = new DataTable();
da.Fill(dat);
con.Close();
return dat;
}
#endregion
#region 返回值为DataTable的参数化
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] sp)
{
//返回的值是一个表
SqlConnection con = new SqlConnection(Connectionstr);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
if (sp != null)
{
da.SelectCommand.Parameters.AddRange(sp);
}
con.Open();
DataTable dat = new DataTable();
da.Fill(dat);
con.Close();
return dat;
}
#endregion
#region 返回值为int类型的 ExecuteNonQuery 简单增删改查
public static int ExecuteNonQuery(string sql)
{
//增删改
SqlConnection con = new SqlConnection(Connectionstr);
SqlCommand com = new SqlCommand(sql, con);
con.Open();
int i = com.ExecuteNonQuery();
con.Close();
return i;
}
#endregion
#region 参数化返回值int类型的ExecuteNonQuery 简单增删改查
public static int ExecuteNonQuery(string sql, params SqlParameter[] sp)
{
//增删改
SqlConnection con = new SqlConnection(Connectionstr);
SqlCommand com = new SqlCommand(sql, con);
if (sp != null)
{
com.Parameters.AddRange(sp);
}
con.Open();
int i = com.ExecuteNonQuery();
con.Close();
return i;
}
#endregion
#region 返回值为sqldatareader的ExecuteReader访问
public static SqlDataReader ExecuteReader(string sql)
{
SqlConnection con = new SqlConnection(Connectionstr);
SqlCommand com = new SqlCommand(sql, con);
con.Open();
return com.ExecuteReader(CommandBehavior.CloseConnection);
}
#endregion
#region 参数化返回值为sqldatareader的ExecuteReader访问
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] sp)
{
SqlConnection con = new SqlConnection(Connectionstr);
SqlCommand com = new SqlCommand(sql, con);
con.Open();
if (sp != null)
{
com.Parameters.AddRange(sp);
}
return com.ExecuteReader(CommandBehavior.CloseConnection);
}
#endregion
#region 更新数据
#endregion
}
2.数据库连接字符串
(1).通过SQL Server 身份验证
<connectionStrings>
<add name="DB" connectionString="server=.;uid=用户;pwd=密码;database=库名"/>
</connectionStrings>
(2).Windows 身份验证
<connectionStrings>
<add name="DB" connectionString="server=服务器名称;integrated security=true;database=库名;"/>
</connectionStrings>