当前位置:首页 > 网络编程 > WEB编程 > ASP.net > DBEngine的类片段(使用自动填充)

DBEngine的类片段(使用自动填充)

点击次数:20 次 发布日期:2008-11-26 23:30:21 作者:源代码网
源代码网推荐 private bool IsInitializeDataAccess()
源代码网推荐         {
源代码网推荐             bool IsInit=false;
源代码网推荐             #region intialize
源代码网推荐             if(this.accessSql ==null)
源代码网推荐             {
源代码网推荐                 if(this.cnnstr!="")
源代码网推荐                 {
源代码网推荐                     this.accessSql =new DataAccessSQL(this.cnnstr);
源代码网推荐                     IsInit =true;
源代码网推荐                 }
源代码网推荐                 else
源代码网推荐                 {
源代码网推荐                     throw new Exception("The Connectstring is null! program can"t create a connect with SQL server.");
源代码网推荐                 }
源代码网推荐             }
源代码网推荐             else
源代码网推荐             {
源代码网推荐                 IsInit =true;
源代码网推荐             }
源代码网推荐             #endregion  
源代码网推荐             return IsInit;
源代码网推荐         }
源代码网推荐         private SqlCommand sqlSPCmd(String spname,Hashtable values)
源代码网推荐         {
源代码网推荐             #region intialize
源代码网推荐             if(!IsInitializeDataAccess())
源代码网推荐                 return null;
源代码网推荐             #endregion
源代码网推荐             SqlCommand cmd= new SqlCommand();
源代码网推荐             cmd.Connection=new SqlConnection(this.cnnstr);
源代码网推荐             cmd.CommandText =spname;
源代码网推荐             cmd.CommandType =CommandType.StoredProcedure;
源代码网推荐             cmd.CommandTimeout =30;        
源代码网推荐             cmd=accessSql.getStroeProcedureParamsByName(cmd)
源代码网推荐             foreach(SqlParameter sp in cmd.Parameters)
源代码网推荐             {               
源代码网推荐                 if(values[sp.ParameterName.Substring(1)].ToString()!="" ||values[sp.ParameterName.Substring(1)]!=null)
源代码网推荐                 {
源代码网推荐                     object o=(object)values[sp.ParameterName.Substring(1)];
源代码网推荐                     cmd.Parameters[sp.ParameterName].Value =o;
源代码网推荐                 }               
源代码网推荐                 else
源代码网推荐                 {
源代码网推荐                     cmd.Parameters[sp.ParameterName].Value =null;
源代码网推荐                 }
源代码网推荐                
源代码网推荐             }        
源代码网推荐             return cmd;
源代码网推荐         }
源代码网推荐 private Hashtable CreateHashTable(string[] cutkeys,object[] cutvalues)
源代码网推荐   {
源代码网推荐    Hashtable newvalues=new Hashtable();
源代码网推荐    for(int i=0;i<cutkeys.Length;i++)
源代码网推荐    {
源代码网推荐     newvalues.Add(cutkeys,cutvalues);
源代码网推荐     Console.WriteLine("key="+cutkeys.ToString()+"|value="+cutvalues.ToString());
源代码网推荐    }
源代码网推荐    return newvalues;
源代码网推荐   }
源代码网推荐   public void ExecuteNonQuery(String spname,string[] capation,Object[] values)
源代码网推荐   {   
源代码网推荐    Hashtable newvalues=this.CreateHashTable(capation,values);   
源代码网推荐    #region build sqlcommand.Parameters and execute command
源代码网推荐    //SqlConnection conn = new SqlConnection(this.cnnstr);
源代码网推荐    SqlCommand noretcmd=this.sqlSPCmd(spname,newvalues);
源代码网推荐    try
源代码网推荐    {
源代码网推荐     noretcmd.Connection.Open();
源代码网推荐     noretcmd.Prepare();
源代码网推荐     noretcmd.ExecuteNonQuery();
源代码网推荐    }
源代码网推荐    catch(SqlException se)
源代码网推荐    {
源代码网推荐      throw(se);
源代码网推荐    }
源代码网推荐    finally
源代码网推荐    {
源代码网推荐     if(noretcmd.Connection.State==ConnectionState.Open )//(conn.State==ConnectionState.Open)
源代码网推荐     {
源代码网推荐      noretcmd.Connection.Close();
源代码网推荐     }
源代码网推荐    }
源代码网推荐    #endregion
源代码网推荐   }

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华