【发布时间】:2016-05-02 16:20:20
【问题描述】:
我一直在尝试为我的网站创建一个基本的内部消息传递系统。它会让我在messages表中插入一个新行,但messages中的sellerID(即itemsupdate表中的userID)返回itemsupdate表中第一行的值。
我希望它将 userID 值插入到 SellerID 字段中。
string message = messagebox.Text;
string itemid = Request.QueryString["itemID"];
string finduser = Session["ID"].ToString();
//comunicate to the web server
string connectionString = WebConfigurationManager.ConnectionStrings["itemsconnection"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
// myConnection.ConnectionString is now set to connectionString.
myConnection.Open();
string messages = "SELECT userID FROM itemsupdate INNER JOIN users ON itemsupdate.userID = users.ID";
SqlCommand command = new SqlCommand(messages, myConnection);
int sellerid = Convert.ToInt32(command.ExecuteScalar().ToString());
//add the above variables to the table 'comments6'
string query = "INSERT INTO messages(message, buyerID, sellerID, itemID) VALUES(@message, @finduser, @sellerid, @itemid)";
SqlCommand myCommand = new SqlCommand(query, myConnection);
//create a parameterised object
myCommand.Parameters.AddWithValue("@finduser", finduser);
myCommand.Parameters.AddWithValue("@sellerid", sellerid);
myCommand.Parameters.AddWithValue("@itemid", itemid);
myCommand.Parameters.AddWithValue("@message", message);
myCommand.ExecuteNonQuery();
myConnection.Close();
我没有收到任何错误消息。任何帮助将非常感激。我对 SQL 和 StackOverflow 也比较陌生,所以会遇到新手。
【问题讨论】:
-
command.ExecuteScalar()根据 select 语句返回一个且仅第一行。 -
好的@t0mm13b,我该如何解决这个问题?
-
您正在尝试循环遍历
itemsupdate表以获取userID?您的 sql 没有意义,您的问题也不清楚,itemsupdate表中的一个卖家有多少行。
标签: c# sql asp.net visual-studio