[asp]代码库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace SqlHelper
{
class SQLHelper
{
public static int ExecuteNonQuery ( string sql,params SqlParameter[] parameters )
{
string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using ( SqlConnection strconn = new SqlConnection ( conn ) )
{
strconn.Open();
using ( SqlCommand cmd = strconn.CreateCommand() )
{
cmd.CommandText = sql;
foreach ( SqlParameter parameter in parameters )
{
cmd.Parameters.Add ( parameter );
}
return cmd.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar ( string sql, params SqlParameter[] parameters )
{
string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using ( SqlConnection strconn = new SqlConnection ( conn ) )
{
strconn.Open();
using ( SqlCommand cmd = strconn.CreateCommand() )
{
cmd.CommandText = sql;
foreach ( SqlParameter parameter in parameters )
{
cmd.Parameters.Add ( parameter );
}
return cmd.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTable ( string sql, params SqlParameter[] parameters )
{
string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using ( SqlConnection strconn = new SqlConnection ( conn ) )
{
strconn.Open();
using ( SqlCommand cmd = strconn.CreateCommand() )
{
cmd.CommandText = sql;
foreach ( SqlParameter parameter in parameters )
{
cmd.Parameters.Add ( parameter );
}
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter ( cmd );
adapter.Fill ( dataset );
return dataset.Tables[0];
}
}
}
}
}
string name = Convert.ToString ( SQLHelper.ExecuteScalar ( "select name from T_persons where mailto:id=@id">id=@id>", new SqlParameter("id", 20)));
by: 发表于:2017-11-14 10:33:26 顶(0) | 踩(0) 回复
??
回复评论