日期:2014-05-19  浏览次数:20917 次

求一个普通点的winform(c#)对sqlserver操作类
请路过的大虾赐教

------解决方案--------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace Document
{
/**//// <summary>
/// Summary description for DataHelper.
/// </summary>
public class DataHelper
{
public DataHelper()
{
//
// TODO: Add constructor logic here
//
}
public static string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings[ "ConnectionString "];

GetDataSet#region GetDataSet
public static DataSet GetDataSet(string sql)
{
SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion

ExecCommand#region ExecCommand
public static int ExecCommand(SqlCommand sqlcom)
{
SqlConnection conn=new SqlConnection(ConnectionString);
sqlcom.Connection =conn;
conn.Open();
try
{
int rtn=sqlcom.ExecuteNonQuery();
return rtn;
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
return 0;

}
public static int ExecCommand(string sql)
{
if (sql.EndsWith( ", ")) sql=sql.Substring(0,sql.Length-1);

SqlCommand sqlcom=new SqlCommand(sql);
return ExecCommand(sqlcom);
}
#endregion

ExecuteScalar#region ExecuteScalar
public static object ExecuteScalar(string sql)
{
SqlConnection conn=new SqlConnection(ConnectionString);
SqlCommand sqlcom=new SqlCommand(sql,conn);
conn.Open();
try
{
object rtn=sqlcom.ExecuteScalar ();
return rtn;
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
return null;
}
#endregion

ExecSPCommand#region ExecSPCommand
public static void ExecSPCommand(string sql,System.Data.IDataParameter[] paramers)
{
SqlConnection conn=new SqlConnection(ConnectionString);
SqlCommand sqlcom=new SqlCommand(sql,conn);
sqlcom.CommandType= CommandType.StoredProcedure ;

foreach(System.Data.IDataParameter paramer in paramers)
{
sqlcom.Parameters.Add(paramer);
}
conn.Open();
try
{
sqlcom.ExecuteNonQuery();
}
catch(Exception ex)
{
string s=ex.Message ;
}
finally
{
conn.Close();
}
}
#endregion

ExecSPDataSet#reg