1.建一个通用的处理数据的类
using
System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace Document
{
/**/ /// <summary>
/// SummarydescriptionforDataHelper.
/// </summary>
public class DataHelper
{
public DataHelper()
{
//
// TODO:Addconstructorlogichere
//
}
public static string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings[ " ConnectionString " ];
GetDataSet #region GetDataSet
public static DataSetGetDataSet( string sql)
{
SqlDataAdaptersda = new SqlDataAdapter(sql,ConnectionString);
DataSetds = new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
ExecCommand #region ExecCommand
public static int ExecCommand(SqlCommandsqlcom)
{
SqlConnectionconn = new SqlConnection(ConnectionString);
sqlcom.Connection = conn;
conn.Open();
try
{
int rtn = sqlcom.ExecuteNonQuery();
return rtn;
}
catch (Exceptionex)
{
throw ex;
}
finally
{
conn.Close();
}
return 0 ;
}
public static int ExecCommand( string sql)
{
if (sql.EndsWith( " , " ))sql = sql.Substring( 0 ,sql.Length - 1 );
SqlCommandsqlcom = new SqlCommand(sql);
return ExecCommand(sqlcom);
}
#endregion
ExecuteScalar #region ExecuteScalar
public static object ExecuteScalar( string sql)
{
SqlConnectionconn = new SqlConnection(ConnectionString);
SqlCommandsqlcom = new SqlCommand(sql,conn);
conn.Open();
try
{
object rtn = sqlcom.ExecuteScalar();
return rtn;
}
catch (Exceptionex)
{
throw ex;
}
finally
{
conn.Close();
}
return null ;
}
#endregion
ExecSPCommand #region ExecSPCommand
public static void ExecSPCommand( string sql,System.Data.IDataParameter[]paramers)
{
SqlConnectionconn = new SqlConnection(ConnectionString);
SqlCommandsqlcom = new SqlCommand(sql,conn);
sqlcom.CommandType = CommandType.StoredProcedure;
foreach (System.Data.IDataParameterparamer in paramers)
{
sqlcom.Parameters.Add(paramer);
}
conn.Open();
try
{
sqlcom.ExecuteNonQuery();
}
catch (Exceptionex)
{
string s = ex.Message;
}
finally
{
conn.Close();
}
}
#endregion
ExecSPDataSet #region ExecSPDataSet
public static DataSetExecSPDataSet( string sql,System.Data.IDataParameter[]paramers)
{
SqlConnectionconn = new SqlConnection(ConnectionString);
SqlCommandsqlcom = new SqlCommand(sql,conn);
sqlcom.CommandType = CommandType.StoredProcedure;
foreach (System.Data.IDataParameterparamer in paramers)
{
sqlcom.Parameters.Add(paramer);
}
conn.Open();
SqlDataAdapterda = new SqlDataAdapter();
da.SelectCommand = sqlcom;
DataSetds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
#endregion
DbType #region DbType
private static System.Data.DbTypeGetDbType(Typetype)
{
DbTyperesult = DbType.String;
if (type.Equals( typeof ( int )) || type.IsEnum)
result = DbType.Int32;
else if (type.Equals( typeof ( long )))
result = DbType.Int32;
else if (type.Equals( typeof ( double )) || type.Equals( typeof (Double)))
result = DbType.Decimal;
else if (type.Equals( typeof (DateTime)))
result = DbType.DateTime;
else if (type.Equals( typeof ( bool )))
result = DbType.Boolean;
else if (type.Equals( typeof ( string )))
result = DbType.String;
else if (type.Equals( typeof ( decimal )))
result = DbType.Decimal;
else if (type.Equals( typeof ( byte [])))
result = DbType.Binary;
else if (type.Equals( typeof (Guid)))
result = DbType.Guid;
return result;
}
#endregion
UpdateTable #region UpdateTable
public static void UpdateTable(DataTabledt, string TableName, string KeyName)
{
foreach (DataRowdr in dt.Rows)
{
updateRow(dr,TableName,KeyName);
}
}
#endregion
InsertTable #region InsertTable
// 用于主键是数据库表名+ID类型的
public static void InsertTable(DataTabledt)
{
string TableName = " [ " + dt.TableName + " ] " ;
string KeyName = dt.TableName + " ID " ;
foreach (DataRowdr in dt.Rows)
{
insertRow(dr,TableName,KeyName);
}
}
// 用于主键是任意类型的
public static void InsertTable(DataTabledt, string KeyName)
{
string TableName = " [ " + dt.TableName + " ] " ;
foreach (DataRowdr in dt.Rows)
{
insertRow(dr,TableName,KeyName);
}
}
#endregion
DeleteTable #region DeleteTable
public static void DeleteTable(DataTabledt, string KeyName)
{
string TableName = " [ " + dt.TableName + " ] " ;
foreach (DataRowdr in dt.Rows)
{
deleteRow(dr,TableName,KeyName);
}
}
#endregion
update
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace Document
{
/**/ /// <summary>
/// SummarydescriptionforDataHelper.
/// </summary>
public class DataHelper
{
public DataHelper()
{
//
// TODO:Addconstructorlogichere
//
}
public static string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings[ " ConnectionString " ];
GetDataSet #region GetDataSet
public static DataSetGetDataSet( string sql)
{
SqlDataAdaptersda = new SqlDataAdapter(sql,ConnectionString);
DataSetds = new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
ExecCommand #region ExecCommand
public static int ExecCommand(SqlCommandsqlcom)
{
SqlConnectionconn = new SqlConnection(ConnectionString);
sqlcom.Connection = conn;
conn.Open();
try
{
int rtn = sqlcom.ExecuteNonQuery();
return rtn;
}
catch (Exceptionex)
{
throw ex;
}
finally
{
conn.Close();
}
return 0 ;
}
public static int ExecCommand( string sql)
{
if (sql.EndsWith( " , " ))sql = sql.Substring( 0 ,sql.Length - 1 );
SqlCommandsqlcom = new SqlCommand(sql);
return ExecCommand(sqlcom);
}
#endregion
ExecuteScalar #region ExecuteScalar
public static object ExecuteScalar( string sql)
{
SqlConnectionconn = new SqlConnection(ConnectionString);
SqlCommandsqlcom = new SqlCommand(sql,conn);
conn.Open();
try
{
object rtn = sqlcom.ExecuteScalar();
return rtn;
}
catch (Exceptionex)
{
throw ex;
}
finally
{
conn.Close();
}
return null ;
}
#endregion
ExecSPCommand #region ExecSPCommand
public static void ExecSPCommand( string sql,System.Data.IDataParameter[]paramers)
{
SqlConnectionconn = new SqlConnection(ConnectionString);
SqlCommandsqlcom = new SqlCommand(sql,conn);
sqlcom.CommandType = CommandType.StoredProcedure;
foreach (System.Data.IDataParameterparamer in paramers)
{
sqlcom.Parameters.Add(paramer);
}
conn.Open();
try
{
sqlcom.ExecuteNonQuery();
}
catch (Exceptionex)
{
string s = ex.Message;
}
finally
{
conn.Close();
}
}
#endregion
ExecSPDataSet #region ExecSPDataSet
public static DataSetExecSPDataSet( string sql,System.Data.IDataParameter[]paramers)
{
SqlConnectionconn = new SqlConnection(ConnectionString);
SqlCommandsqlcom = new SqlCommand(sql,conn);
sqlcom.CommandType = CommandType.StoredProcedure;
foreach (System.Data.IDataParameterparamer in paramers)
{
sqlcom.Parameters.Add(paramer);
}
conn.Open();
SqlDataAdapterda = new SqlDataAdapter();
da.SelectCommand = sqlcom;
DataSetds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
#endregion
DbType #region DbType
private static System.Data.DbTypeGetDbType(Typetype)
{
DbTyperesult = DbType.String;
if (type.Equals( typeof ( int )) || type.IsEnum)
result = DbType.Int32;
else if (type.Equals( typeof ( long )))
result = DbType.Int32;
else if (type.Equals( typeof ( double )) || type.Equals( typeof (Double)))
result = DbType.Decimal;
else if (type.Equals( typeof (DateTime)))
result = DbType.DateTime;
else if (type.Equals( typeof ( bool )))
result = DbType.Boolean;
else if (type.Equals( typeof ( string )))
result = DbType.String;
else if (type.Equals( typeof ( decimal )))
result = DbType.Decimal;
else if (type.Equals( typeof ( byte [])))
result = DbType.Binary;
else if (type.Equals( typeof (Guid)))
result = DbType.Guid;
return result;
}
#endregion
UpdateTable #region UpdateTable
public static void UpdateTable(DataTabledt, string TableName, string KeyName)
{
foreach (DataRowdr in dt.Rows)
{
updateRow(dr,TableName,KeyName);
}
}
#endregion
InsertTable #region InsertTable
// 用于主键是数据库表名+ID类型的
public static void InsertTable(DataTabledt)
{
string TableName = " [ " + dt.TableName + " ] " ;
string KeyName = dt.TableName + " ID " ;
foreach (DataRowdr in dt.Rows)
{
insertRow(dr,TableName,KeyName);
}
}
// 用于主键是任意类型的
public static void InsertTable(DataTabledt, string KeyName)
{
string TableName = " [ " + dt.TableName + " ] " ;
foreach (DataRowdr in dt.Rows)
{
insertRow(dr,TableName,KeyName);
}
}
#endregion
DeleteTable #region DeleteTable
public static void DeleteTable(DataTabledt, string KeyName)
{
string TableName = " [ " + dt.TableName + " ] " ;
foreach (DataRowdr in dt.Rows)
{
deleteRow(dr,TableName,KeyName);
}
}
#endregion
update
发表评论
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元
评论