日期:2014-05-18  浏览次数:20431 次

SQL插入语句太长,请高手指点比较好的解决办法.
眼睛都看花了,请朋帮我对一下字段数与参数个数. 

查询值的数目与目标字段中的数目不同。  
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  

异常详细信息: System.Data.OleDb.OleDbException: 查询值的数目与目标字段中的数目不同。 

源错误:  


行 265: OleDbCommand cmdInsertUser = new OleDbCommand("INSERT INTO Dv_User(UserName,UserPassword,UserEmail,UserFace,UserWidth,UserHeight,UserIM,UserClass,userWealth,userEP,userCP,UserQuesion,UserAnswer,UserFav,UserInfo,UserSetting,UserGroupID,TitlePic,UserMsg,TruePassWord,UserToday) VALUES( '" + txtUserName.Text + " ', '" + MD5(txtPassword.Text) + " ', '" + txtUserName.Text + " ', '@domain.com ', 'Images/userface/image1.gif ',32,32, ' ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ', '新手上路 ',100,60,30, 'a ', 'b ', '陌生人,我的好友,黑名单 ', ' ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ', '1 ¦ ¦ ¦0 ¦ ¦ ¦0 ',9, 'level0.gif ', '0 ¦ ¦0 ¦ ¦null ', '5738509MLGn36NdG ', '0 ¦0 ¦0 ¦0 ¦0 ')", accessConn); 
行 266: accessConn.Open(); 
行 267: cmdInsertUser.ExecuteNonQuery(); 
行 268: accessConn.Close(); 
行 269: cmdInsertUser.Dispose(); 
  

源文件: e:\n\3CMarket\ascx\member_Logins.ascx.cs 行: 267

------解决方案--------------------
你可以把要插入的值赋值给变量啊,比如
string a=txtUserName.Text
string b=MD5(txtPassword.Text)
然后insert(........)Values('"+a+"','"+b+"')
这样简短了很多吧

------解决方案--------------------
你用System.Text.StringBuilder先把字符串拼好 ,不就行了
------解决方案--------------------
用string +=拼接 

或用stringbuilder

C# code

string b = "h";
b += "ello";

------解决方案--------------------
用String.Format()吧
------解决方案--------------------
C# code
string sql = string.Format
(
@"
INSERT
INTO Dv_User
(
  UserName,    UserPassword, UserEmail,  UserFace,   UserWidth,
  UserHeight,  UserIM,       UserClass,  userWealth, userEP,
  userCP,      UserQuesion,  UserAnswer, UserFav,    UserInfo,
  UserSetting, UserGroupID,  TitlePic,   UserMsg,    TruePassWord,
  UserToday
)
VALUES
(
  '{0}', '{1}', '{0}@domain.com', 'Images/userface/image1.gif', 32,
  32, ' | | | | | | | | | | | | | | | | | | ', '新手上路', 100, 60,
  30, 'a', 'b', '陌生人,我的好友,黑名单', ' | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ',
  '1 | | |0 | | |0', 9, 'level0.gif  ', '0 | |0 | |null', '5738509MLGn36NdG',
  '0 |0 |0 |0 |0  '
)
",
txtUserName.Text, MD5(txtPassword.Text)
);
OleDbCommand cmdInsertUser = new OleDbCommand(sql, accessConn);