2023年6月21日发(作者:)

通⽤的sql插⼊与修改语句,⼀劳永逸此⽅法只要传⼊表名,字段名,跟值就好了,不⽤写sql语句,很⼤程度的节省了开发时间,与代码量建⼀个类⽂件,⽅便调⽤, ///

/// 执⾏sql插⼊语句,返回受影响的⾏数 /// /// 要插⼊的表名 /// 操作类型 /// 字段名数组 /// public int Insert(string TableName, CommandType ct,Dictionary dic) { string str1 = "", str2 = ""; if ( > 0) { open(); SqlCommand comm = new SqlCommand(); foreach (KeyValuePair kvp in dic)//遍历数组的key,value; { (new SqlParameter("@" + , )); if (str1 == "" && str2 == "") { str1 += ; str2 += "@" + ; } else { str1 += "," + ; str2 += ",@" + ; } } try { dText = "insert into " + TableName + "(" + str1 + ") values (" + str2 + ")"; tion = conn; dType = ct; int i = eNonQuery(); return i; } catch { return 0; } finally { close(); } } else { return 0; } } /// /// 执⾏sql修改语句,返回受影响的⾏ /// /// 要修改的表名 /// 操作类型 /// 字段名数组 /// 条件语句 /// public int Update(string TableName, CommandType ct, Dictionary dic, string where) { string str = ""; if ( > 0) { open(); SqlCommand comm = new SqlCommand(); foreach (KeyValuePair kvp in dic) { (new SqlParameter("@" + , )); if (str == "") { str += + "=@" + ; } else { str += "," + + "=@" + ; } } try { dText = "update " + TableName + " set " + str + " where " + where; tion = conn; dType = ct; int i = eNonQuery(); return i; } catch { return 0; } finally { close(); } } else { return 0; } }上⾯是类⽂件的代码,这⾥是调⽤的⽅法 Dictionary dic = new Dictionary(); ("Title", ProductTitle); ("addtime", AddTime); //新建⼀个Dictionary数组,第⼀个参数为键值,我们⽤来传字段名; //第⼆个参数为value值,我们⽤来传对应的字段的值; ("SW_Product", , dic); //调⽤插⼊的⽅法,第⼀个为表名,第⼆个为操作类型,第三个为数组

int id = g(tring["id"].ToString()); ("SW_Product", , dic, "id=" + id); //调⽤修改的⽅法,第⼀个为表名,第⼆个为操作类型,第三个为数组,第四个为条件语句 如果⼤家还有更好的⽅法,欢迎留⾔,⼀起探讨;

2023年6月21日发(作者:)

通⽤的sql插⼊与修改语句,⼀劳永逸此⽅法只要传⼊表名,字段名,跟值就好了,不⽤写sql语句,很⼤程度的节省了开发时间,与代码量建⼀个类⽂件,⽅便调⽤, ///

/// 执⾏sql插⼊语句,返回受影响的⾏数 /// /// 要插⼊的表名 /// 操作类型 /// 字段名数组 /// public int Insert(string TableName, CommandType ct,Dictionary dic) { string str1 = "", str2 = ""; if ( > 0) { open(); SqlCommand comm = new SqlCommand(); foreach (KeyValuePair kvp in dic)//遍历数组的key,value; { (new SqlParameter("@" + , )); if (str1 == "" && str2 == "") { str1 += ; str2 += "@" + ; } else { str1 += "," + ; str2 += ",@" + ; } } try { dText = "insert into " + TableName + "(" + str1 + ") values (" + str2 + ")"; tion = conn; dType = ct; int i = eNonQuery(); return i; } catch { return 0; } finally { close(); } } else { return 0; } } /// /// 执⾏sql修改语句,返回受影响的⾏ /// /// 要修改的表名 /// 操作类型 /// 字段名数组 /// 条件语句 /// public int Update(string TableName, CommandType ct, Dictionary dic, string where) { string str = ""; if ( > 0) { open(); SqlCommand comm = new SqlCommand(); foreach (KeyValuePair kvp in dic) { (new SqlParameter("@" + , )); if (str == "") { str += + "=@" + ; } else { str += "," + + "=@" + ; } } try { dText = "update " + TableName + " set " + str + " where " + where; tion = conn; dType = ct; int i = eNonQuery(); return i; } catch { return 0; } finally { close(); } } else { return 0; } }上⾯是类⽂件的代码,这⾥是调⽤的⽅法 Dictionary dic = new Dictionary(); ("Title", ProductTitle); ("addtime", AddTime); //新建⼀个Dictionary数组,第⼀个参数为键值,我们⽤来传字段名; //第⼆个参数为value值,我们⽤来传对应的字段的值; ("SW_Product", , dic); //调⽤插⼊的⽅法,第⼀个为表名,第⼆个为操作类型,第三个为数组

int id = g(tring["id"].ToString()); ("SW_Product", , dic, "id=" + id); //调⽤修改的⽅法,第⼀个为表名,第⼆个为操作类型,第三个为数组,第四个为条件语句 如果⼤家还有更好的⽅法,欢迎留⾔,⼀起探讨;