第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將變量插入表中時(shí)出現(xiàn)語法錯(cuò)誤Visual Studios 2015 C#

將變量插入表中時(shí)出現(xiàn)語法錯(cuò)誤Visual Studios 2015 C#

C#
守著星空守著你 2021-04-29 10:11:20
目前,我對(duì)嘗試將答案保存/插入數(shù)據(jù)庫中的表時(shí)的當(dāng)前語法錯(cuò)誤問題的含義感到困惑。當(dāng)我嘗試使用硬編碼的變量時(shí),它運(yùn)行良好,但現(xiàn)在不是這種情況。錯(cuò)誤消息的一部分:附加信息:')'附近的語法不正確不知道我在做什么錯(cuò)。以下是我正在使用的代碼以及錯(cuò)誤所指向的位置。感謝您提供的任何幫助和說明。protected void btnSaveAnswers_Click(object sender, EventArgs e){        Int32 int32StudentID = Convert.ToInt32(Session["StudentID"]);        Int32 int32QuestionID = Convert.ToInt32(Session["QuestionID"]);        String strAnswer = "";        // Save the student's answer to the Answer table.        // Develop the SQL call.        String strSQL = "";        strSQL = "INSERT ";        strSQL += "INTO Answer ";        strSQL += " (StudentID, QuestionID, Answer) ";        strSQL += "VALUES ";        strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", " + strAnswer + ")";        // Define the network connection to the SQL Server database.        SqlConnection objSqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["OPT"].ConnectionString);        // Create the SQL command object.        SqlCommand objSqlCommand = new SqlCommand();        objSqlCommand.Connection = objSqlConnection;        objSqlCommand.CommandType = CommandType.Text;        objSqlCommand.CommandText = strSQL;        // Open the connection.        objSqlConnection.Open();        // Execute the Insert statement.        objSqlCommand.ExecuteNonQuery();        // Close the connection.        objSqlConnection.Close();        this.Master.MessageForeColor = System.Drawing.Color.White;        this.Master.Message = "You have saved your answer for this question, click next to continue.";    }
查看完整描述

3 回答

?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

錯(cuò)誤在這一行

strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", " + strAnswer + ")";

根據(jù)您的SQL查詢和數(shù)據(jù)庫,該字段Answer是類型varchar或的字段nvarchar。此類型的字段始終采用字符串類型的值。這已經(jīng)由您完成了。但是SQL Server數(shù)據(jù)庫在單引號(hào)內(nèi)接受這些值''。因此,您的解決方案是

strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";

因?yàn)槲?code>strAnswer在的末尾添加了單引號(hào)strAnswer


查看完整回答
反對(duì) 回復(fù) 2021-05-15
?
侃侃爾雅

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊

我同意有關(guān)字符串串聯(lián)的評(píng)論。如果必須在代碼中編寫SQL查詢,則應(yīng)使用用戶字符串插值。

如果必須這樣做,我會(huì)這樣寫:

String strSQL = $"INSERT INTO Answer  (StudentID, QuestionID, Answer) VALUES ( {int32StudentID}, {int32QuestionID}, '{strAnswer}')";

就是說,這不是為什么您會(huì)出現(xiàn)語法錯(cuò)誤。您在字符串變量周圍缺少單引號(hào)。試試這個(gè):

strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";


查看完整回答
反對(duì) 回復(fù) 2021-05-15
  • 3 回答
  • 0 關(guān)注
  • 241 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)