62,269
社区成员
发帖
与我相关
我的任务
分享insert into carorigin(car_c_number,carorigin_responsiblename,bproid,bcityid,bareaid,carorigin_startplace,tproid,tcityid,tareaid,carorigin_goalplace,car_user_name,carorigin_responsibletelephone,carorigin_heavy_validity,goodspay,car_payment,carorigin_goods_h,carorigin_weight_price,carorigin_softprice,carorigin_msg_validity) values('" + car_c_number.Text + "','" + carorigin_responsiblename.Text + "','" + ddl1.Text + "','" + Request.Form["ddl2"].ToString() + "','" + Request.Form["ddl3"].ToString() + "','" + carorigin_startplace.Text + "','" + ddl4.Text + "','" + Request.Form["ddl5"].ToString() + "','" + Request.Form["ddl6"].ToString() + "','" + carorigin_goalplace.Text + "','" + standard_tbx.Text + "','" + car_need_tbx.Text + "','" + get_send_dr.SelectedValue + "','" + price_tbx.Text + "','" + pay_type_tbx.Text + "','" + need_type_tbx.Text + "','" + contact_type_tbx.Text + "','" + expiry_tbx.Text + "',getdate(),'Show','正常')"
public static int TableInsert()
{
HashTable d = new HashTable();
d.Add("kye","Value");
return this.funInsert(d,"表明");//上面的方法
//用HashTable是因为他好用写法简单开发容易快捷,你用其他的也行,当然你也可以重载一个对象进来坐orm
}

/// <summary>
/// 执行插入操作
/// </summary>
/// <param name="dict">实现IDictionary接口的,比如HashTable,用来表名要插入的字段及要插入的值</param>
/// <param name="strTableName">要插入的表名</param>
/// <returns>执行插入操作影响的行数</returns>
public static int funInsert(IDictionary dict,string strTableName)
{
if(dict==null||dict.Keys.Count==0) return 0;
if (strTableName==null || strTableName.Trim()=="") return 0;
int rows=0;
int num=dict.Count;
string[] Key=new string[num];
string[] Value=new string[num];
string strSql=@"insert into "+strTableName;
string strKeys="";
string strValues="";
int i=0;
foreach(DictionaryEntry entry in dict)
{
Key[i]=entry.Key.ToString();
Value[i]=entry.Value.ToString();
strKeys+=Key[i].Trim()+",";
strValues+="@"+Key[i].Trim()+",";
i++;
}
if(strKeys.Length>1)
{
strKeys=strKeys.Substring(0,strKeys.Length-1);
}
if(strValues.Length>1)
{
strValues=strValues.Substring(0,strValues.Length-1);
}
if(strKeys.Length<=0 || strValues.Length<=0) return 0;
strSql+="("+strKeys+")"+" values ("+strValues+");";
SqlParameter[] param = new SqlParameter[num];
for(int j=0;j<num;j++)
{
param[j]=new SqlParameter("@"+Key[j],Value[j]);
}
using(SqlConnection conn=new SqlConnection(DB_Access.clsConfigOperate.CustomerSqlConnection()))
{
conn.Open();
rows=DB_Access.SqlHelper.ExecuteNonQuery(conn,CommandType.Text,strSql,param);
conn.Close();
}
return rows;