私は自分のウェブサイト用の基本的な内部メッセージングシステムを作成しようとしています。メッセージテーブルに新しい行を挿入することができますが、メッセージのsellerID(itemsupdateテーブルのuserID)はitemsupdateテーブルの最初の行の値を返します。私のSQL文はデータベースから最初の行を読み込みます
私は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
Ok @ t0mm13b、どうすれば修正できますか? –
'itemsupdate'テーブルを' userID'のためにループしようとしていますか?あなたのSQLは意味をなさないし、あなたの質問も明確ではない、 'itemsupdate'テーブル内の1人の売り手のためにいくつの行があるのか。 – t0mm13b