Whats wrong with this SQLCe Query?

帅比萌擦擦* 提交于 2019-12-14 02:12:58

问题


in the past hour i have been trying different variants of this query but i get error at the username , and the username is just a normal string with username that i get from xml file containing no special characters or whatsoever

I'm using SLQ compact 3.5

P.S i tried to use ? instead of @username also not working all feilds are "nchar" except "date"

                C = nodeItem["user_from"].InnerText;
                avatar = nodeItem["user_from_avatar"].InnerText;
                string msgText = nodeItem["message"].InnerText;
                DateTime dt=DateTime.Now;

                string sql = "INSERT INTO posts(user,msg,avatar,date) VALUES(@username,@messige,@userpic,@thedate)";
                using (SqlCeCommand cmd = new SqlCeCommand(sql, connection))
                {
                    cmd.Parameters.Add("@username",C);
                    cmd.Parameters.Add("@messige", msgText.ToString());
                    cmd.Parameters.Add("@userpic", avatar.ToString());
                    cmd.Parameters.Add("@thedate", dt);
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    adapter.Update(data);
                    connection.Close();
                }

The error msg :
(source: clip2net.com)
Thanks , Nikola


回答1:


Try surrounding 'user' with square brackets, '[user]' (and possibly date).




回答2:


In SqlCe u cannot use directly the parameters I mean the sentence:

string sql = "INSERT INTO posts(user,msg,avatar,date) VALUES(@username,@messige,@userpic,@thedate)";

is wrong. it should be

string sql = "INSERT INTO posts(user,msg,avatar,date) VALUES(?,?,?,?)";

and then followed by:

using (SqlCeCommand cmd = new SqlCeCommand(sql, connection))
                {
                    cmd.Parameters.Add("@username",C);
                    cmd.Parameters.Add("@messige", msgText.ToString());
                    cmd.Parameters.Add("@userpic", avatar.ToString());
                    cmd.Parameters.Add("@thedate", dt);
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    adapter.Update(data);
                    connection.Close();
                }

In SqlCe the parameters are passed as "?"

Hope it helps you



来源:https://stackoverflow.com/questions/1125846/whats-wrong-with-this-sqlce-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!