DataHelper

 1.建一个通用的处理数据的类

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Text;
None.gif
namespace  Document
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for DataHelper.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DataHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public DataHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: Add constructor logic here
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
public static string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
InBlock.gif    
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetDataSet#region GetDataSet
InBlock.gif        
public static DataSet GetDataSet(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlDataAdapter    sda 
=new SqlDataAdapter(sql,ConnectionString);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            sda.Fill(ds);
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExecCommand#region ExecCommand
InBlock.gif        
public static int ExecCommand(SqlCommand sqlcom)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn
=new SqlConnection(ConnectionString);
InBlock.gif            sqlcom.Connection 
=conn;
InBlock.gif            conn.Open();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int rtn=sqlcom.ExecuteNonQuery();
InBlock.gif                
return rtn;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return 0;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static int ExecCommand(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (sql.EndsWith(",")) sql=sql.Substring(0,sql.Length-1);
InBlock.gif        
InBlock.gif            SqlCommand sqlcom
=new SqlCommand(sql);
InBlock.gif            
return ExecCommand(sqlcom);                
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExecuteScalar#region ExecuteScalar
InBlock.gif        
public static object ExecuteScalar(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn
=new SqlConnection(ConnectionString);
InBlock.gif            SqlCommand sqlcom
=new SqlCommand(sql,conn);
InBlock.gif            conn.Open();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object rtn=sqlcom.ExecuteScalar ();
InBlock.gif                
return rtn;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExecSPCommand#region ExecSPCommand
InBlock.gif        
public static void ExecSPCommand(string sql,System.Data.IDataParameter[] paramers)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn
=new SqlConnection(ConnectionString);
InBlock.gif            SqlCommand sqlcom
=new SqlCommand(sql,conn);
InBlock.gif            sqlcom.CommandType
= CommandType.StoredProcedure ;
InBlock.gif
InBlock.gif            
foreach(System.Data.IDataParameter paramer in paramers)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlcom.Parameters.Add(paramer);
ExpandedSubBlockEnd.gif            }
            
InBlock.gif            conn.Open();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlcom.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string s=ex.Message ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExecSPDataSet#region ExecSPDataSet
InBlock.gif        
public static DataSet ExecSPDataSet(string sql,System.Data.IDataParameter[] paramers)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn
=new SqlConnection(ConnectionString);
InBlock.gif            SqlCommand sqlcom
=new SqlCommand(sql,conn);
InBlock.gif            sqlcom.CommandType
= CommandType.StoredProcedure ;
InBlock.gif
InBlock.gif            
foreach(System.Data.IDataParameter paramer in paramers)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlcom.Parameters.Add(paramer);
ExpandedSubBlockEnd.gif            }
            
InBlock.gif            conn.Open();
InBlock.gif            
InBlock.gif            SqlDataAdapter da
=new SqlDataAdapter();
InBlock.gif            da.SelectCommand
=sqlcom;
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            da.Fill(ds);
InBlock.gif        
InBlock.gif            conn.Close();
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DbType#region DbType
InBlock.gif        
private static System.Data.DbType GetDbType(Type type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DbType result 
= DbType.String;
InBlock.gif            
if( type.Equals(typeof(int)) ||  type.IsEnum)
InBlock.gif                result 
= DbType.Int32;
InBlock.gif            
else if( type.Equals(typeof(long)))
InBlock.gif                result 
= DbType.Int32;
InBlock.gif            
else if( type.Equals(typeof(double)) || type.Equals( typeof(Double)))
InBlock.gif                result 
= DbType.Decimal;
InBlock.gif            
else if( type.Equals(typeof(DateTime)))
InBlock.gif                result 
= DbType.DateTime;
InBlock.gif            
else if( type.Equals(typeof(bool)))
InBlock.gif                result 
= DbType.Boolean;
InBlock.gif            
else if( type.Equals(typeof(string) ) )
InBlock.gif                result 
= DbType.String;
InBlock.gif            
else if( type.Equals(typeof(decimal)))
InBlock.gif                result 
= DbType.Decimal;
InBlock.gif            
else if( type.Equals(typeof(byte[])))
InBlock.gif                result 
= DbType.Binary;
InBlock.gif            
else if( type.Equals(typeof(Guid)))
InBlock.gif                result 
= DbType.Guid;
InBlock.gif        
InBlock.gif            
return result;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
UpdateTable#region UpdateTable
InBlock.gif        
public static void UpdateTable(DataTable dt,string TableName,string KeyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach(DataRow dr in dt.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                updateRow(dr,TableName,KeyName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
InsertTable#region InsertTable
InBlock.gif        
//用于主键是数据库表名+ID类型的
InBlock.gif
        public static void InsertTable(DataTable dt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string TableName="["+dt.TableName+"]";
InBlock.gif            
string KeyName=dt.TableName+"ID";
InBlock.gif            
foreach(DataRow dr in dt.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                insertRow(dr,TableName,KeyName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//用于主键是任意类型的
InBlock.gif
        public static void InsertTable(DataTable dt,string KeyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string TableName="["+dt.TableName+"]";
InBlock.gif            
foreach(DataRow dr in dt.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                insertRow(dr,TableName,KeyName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DeleteTable#region DeleteTable
InBlock.gif        
public static void DeleteTable(DataTable dt,string KeyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string TableName="["+dt.TableName+"]";
InBlock.gif            
foreach(DataRow dr in dt.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                deleteRow(dr,TableName,KeyName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
updateRow#region updateRow
InBlock.gif        
private static void  updateRow(DataRow dr,string TableName,string KeyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (dr[KeyName]==DBNull.Value ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception(KeyName +"的值不能为空");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
if (dr.RowState ==DataRowState.Deleted)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                deleteRow(dr,TableName,KeyName);
InBlock.gif 
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (dr.RowState ==DataRowState.Modified )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                midifyRow(dr,TableName,KeyName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (dr.RowState ==DataRowState.Added  )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                insertRow(dr,TableName,KeyName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (dr.RowState ==DataRowState.Unchanged )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                midifyRow(dr,TableName,KeyName);
ExpandedSubBlockEnd.gif            }
           
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
deleteRow#region deleteRow
InBlock.gif        
private static void  deleteRow(DataRow dr,string TableName,string KeyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sql="Delete {0} where {1} =@{1}";
InBlock.gif            DataTable dtb
=dr.Table ;
InBlock.gif            sql
=string.Format(sql,TableName,KeyName);
InBlock.gif
InBlock.gif            SqlCommand sqlcom
=new SqlCommand(sql);
InBlock.gif            System.Data.IDataParameter iparam
=new  SqlParameter();
InBlock.gif            iparam.ParameterName    
= "@"+ KeyName;
InBlock.gif            iparam.DbType            
= GetDbType(dtb.Columns[KeyName].DataType);
InBlock.gif            iparam.Value            
= dr[KeyName];
InBlock.gif            sqlcom.Parameters .Add(iparam);
InBlock.gif            
InBlock.gif            ExecCommand(sqlcom);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
midifyRow#region midifyRow
InBlock.gif        
private static void  midifyRow(DataRow dr,string TableName,string KeyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string UpdateSql            = "Update {0} set {1} {2}";
InBlock.gif            
string setSql="{0}= @{0}";
InBlock.gif            
string wherSql=" Where {0}=@{0}";
InBlock.gif            StringBuilder setSb    
= new StringBuilder();
InBlock.gif
InBlock.gif            SqlCommand sqlcom
=new SqlCommand();
InBlock.gif            DataTable dtb
=dr.Table;
InBlock.gif        
InBlock.gif            
for (int k=0; k<dr.Table.Columns.Count; ++k)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Data.IDataParameter iparam
=new  SqlParameter();
InBlock.gif                iparam.ParameterName    
= "@"+ dtb.Columns[k].ColumnName;
InBlock.gif                iparam.DbType            
= GetDbType(dtb.Columns[k].DataType);
InBlock.gif                iparam.Value            
= dr[k];
InBlock.gif                sqlcom.Parameters .Add(iparam);
InBlock.gif
InBlock.gif                
if (dtb.Columns[k].ColumnName==KeyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    wherSql
=string.Format(wherSql,KeyName);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    setSb.Append(
string.Format(setSql,dtb.Columns[k].ColumnName));    
InBlock.gif                    setSb.Append(
",");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
string setStr=setSb.ToString();
InBlock.gif            setStr
=setStr.Substring(0,setStr.Length -1); //trim ,
InBlock.gif
            
InBlock.gif            
string sql = string.Format(UpdateSql, TableName, setStr,wherSql);
InBlock.gif            sqlcom.CommandText 
=sql;    
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ExecCommand(sqlcom);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;            
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
insertRow#region insertRow
InBlock.gif        
private static void  insertRow(DataRow dr,string TableName,string KeyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string InsertSql = "Insert into {0}({1}) values({2})";
InBlock.gif            SqlCommand sqlcom
=new SqlCommand();
InBlock.gif            DataTable dtb
=dr.Table ;
InBlock.gif            StringBuilder insertValues    
= new StringBuilder();
InBlock.gif            StringBuilder cloumn_list    
= new StringBuilder();
InBlock.gif            
for (int k=0; k<dr.Table.Columns.Count; ++k)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//just for genentae,
InBlock.gif
                if (dtb.Columns[k].ColumnName==KeyName) continue;
InBlock.gif                System.Data.IDataParameter iparam
=new  SqlParameter();
InBlock.gif                iparam.ParameterName    
= "@"+ dtb.Columns[k].ColumnName;
InBlock.gif                iparam.DbType            
= GetDbType(dtb.Columns[k].DataType);
InBlock.gif                iparam.Value            
= dr[k];
InBlock.gif                sqlcom.Parameters .Add(iparam);
InBlock.gif
InBlock.gif                cloumn_list.Append(dtb.Columns[k].ColumnName);
InBlock.gif                insertValues.Append(
"@"+dtb.Columns[k].ColumnName);
InBlock.gif
InBlock.gif                cloumn_list.Append(
",");
InBlock.gif                insertValues.Append(
",");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
string cols=cloumn_list.ToString();
InBlock.gif            cols
=cols.Substring(0,cols.Length -1);
InBlock.gif
InBlock.gif            
string values=insertValues.ToString();
InBlock.gif            values
=values.Substring(0,values.Length -1);
InBlock.gif            
InBlock.gif            
string sql = string.Format(InsertSql, TableName,cols ,values);
InBlock.gif            sqlcom.CommandText 
=sql;    
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ExecCommand(sqlcom);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
2..调用范例
ContractedBlock.gif ExpandedBlockStart.gif          Insert #region Insert
InBlock.gif        
private void InsertUserInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt
=ds.Tables[0];
InBlock.gif            dt.TableName
="UserInfo";
                string keyname="UserInfoID";
InBlock.gif            DataRow dr
=dt.NewRow();
InBlock.gif            dr[
"LoginName"]=this.txtUserName.Value;
InBlock.gif            dr[
"Pass"]=this.txtPassword.Value;
InBlock.gif            dr[
"NickName"]=this.txtNickName.Value;
InBlock.gif            dr[
"UserType"]=1;
InBlock.gif            dr[
"IsActive"]=false;
InBlock.gif            dr[
"RegisterDate"]=System.DateTime.Now;
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif            dt.AcceptChanges();
InBlock.gif            DataHelper.InsertTable(dt,keyname);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
Update #region Update
InBlock.gif        
private void UpdateUserInfo(string UserID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            DataSet ds
=GetUserOther(UserID);
InBlock.gif            DataTable dt
=ds.Tables[0];
InBlock.gif            dt.TableName
="UserInfo";
                string keyname="UserID";
InBlock.gif            DataRow dr
=dt.Rows[0];
InBlock.gif            dr[
"LoginName"]=this.txtUserName.Value;
InBlock.gif            dr[
"Pass"]=this.txtPassword.Value;
InBlock.gif            dr[
"NickName"]=this.txtNickName.Value;
InBlock.gif            dr[
"UserType"]=1;
InBlock.gif            dr[
"IsActive"]=false;
InBlock.gif            dr[
"RegisterDate"]=System.DateTime.Now;
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif            dt.AcceptChanges();
InBlock.gif            DataHelper.UpdateTable(dt,dt.TableName,keynanme);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
Delete #region Delete
InBlock.gif        
private void DeleteUserInfo(string UserID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            DataSet ds
=GetUserOther(UserID);
InBlock.gif            DataTable dt
=ds.Tables[0];
InBlock.gif            dt.TableName
="UserInfo";            
                string keyname="UserID";
InBlock.gif            DataHelper.DeleteTable(dt,keyname);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

转载于:https://www.cnblogs.com/cheatlove/articles/405379.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值