日期:2009-07-25  浏览次数:20486 次

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[i],cutvalues[i]);
Console.WriteLine("key="+cutkeys[i].ToString()+"|value="+cutvalues[i].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
}