c# - 从 asp.net 中的表单插入到表中

标签 c# asp.net sql database

我有一个页面,您可以在其中填写一些信息,然后根据该信息向数据库中插入一个新行。这是填写的表格的屏幕截图:

enter image description here

这是我在点击提交按钮时插入数据库的代码:

 protected void CreateCourseButton_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=University;Integrated Security=True;Pooling=False";

    string query1 = "insert into Courses(CRN,CourseName,StudyLevel,Capacity,Instructor,Credits,Prerequisite) values ("
        + courseID.Text + "," + courseName.Text + "," + studyLevel.SelectedValue + "," + capacity.Text + "," + "Admin," + credits.Text + "," + prereq.Text + ")";



    SqlCommand cmd1 = new SqlCommand(query1, con);
    con.Open();
    cmd1.ExecuteNonQuery();
    con.Close();
}

问题是,当我点击提交时出现以下错误:

Server Error in '/Bannerweb' Application.

Incorrect syntax near the keyword 'to'.

Description: An unhandled exception occurred during the execution of the current web     
request. Please review the stack trace for more information about the error and where   
it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the   
keyword 'to'.

Source Error: 


Line 32:         SqlCommand cmd1 = new SqlCommand(query1, con);
Line 33:         con.Open();
Line 34:         cmd1.ExecuteNonQuery();
Line 35:         con.Close();
Line 36:     }

Source File: c:\Banner\Bannerweb\Pages\CreateCourse.aspx.cs    Line: 34 

Stack Trace: 


[SqlException (0x80131904): Incorrect syntax near the keyword 'to'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean     
breakConnection) +2084930
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean    
breakConnection) +5084668
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler,   
SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject  
stateObj) +2275
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean 
async) +228
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result,     
String methodName, Boolean sendToPipe) +326
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
   CreateCourse.CreateCourseButton_Click(Object sender, EventArgs e) in  
c:\Banner\Bannerweb\Pages\CreateCourse.aspx.cs:34
  System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112

第 34 行是:

cmd1.ExecuteNonQuery();

谁能帮我解决这个错误?

谢谢

最佳答案

发生此错误是因为您在插入的值之间缺少“”。无论如何,最好的方法是像这样使用 Parameters 集合:

string query1 = "insert into Courses(CRN,CourseName,StudyLevel,Capacity,Instructor,Credits,Prerequisite) values (@crn, @cursename, @studylevel, @capacity, @instructor, @credits, @prerequesite)";

SqlCommand cmd1 = new SqlCommand(query1, con);
cmd1.Parameters.AddWithValue("@crn", courseID.Text);
//add the rest

con.Open();
cmd1.ExecuteNonQuery();
con.Close();

关于c# - 从 asp.net 中的表单插入到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16541337/

相关文章:

c# - 在C#中,可能是Image.SaveAdd的一个bug,谁能帮我解决一下?

c# - 如何将数据写入excel文件并在asp.net中下载

c# - 有什么办法可以改善 TCP 连接时间吗?

javascript - 从 javascript 调用 ASP.NET MVC 应用程序的 URL

mysql - 如何设计具有撤消重做功能的 SQL 数据库?

java - "UcanaccessSQLException: unexpected token"当列名包含撇号时

c# - Epplus 不读取 excel 文件

jquery - 使用 Get 进行 ajax 调用会出现 500 错误

c# - 使用 asp.net 文本框进行 linq to sql 更新的问题

mysql - MIN() 导致 MySQL 返回全空行而不是 0 行