在asp.net中动态创建表和插入数据

时间:2012-07-25 04:35:06

标签: asp.net sql-server vb.net

动态创建表并将数据插入到创建的表中,我使用以下命令动态创建表并插入值。我分别评论了变量的样本值。

Dim question, opt1, opt2, opt3, opt4, ans, paper As String
          question = Request.QueryString("question") '<p>This is my first question</p>
          opt1 = Request.QueryString("option1") ' option 1
          opt2 = Request.QueryString("option2") ' option 2
          opt3 = Request.QueryString("option3") ' option 3
          opt4 = Request.QueryString("option4") ' option 4
          ans = Request.QueryString("answer")    ' 2
          paper = Request.QueryString("paper")  ' cs-5-cder-2012
          'Response.Write("....")
          Dim cs As String = ConfigurationManager.ConnectionStrings("eExamSolutionConnection").ConnectionString
          Dim cn As New SqlConnection(cs)
      Dim cmd As New SqlCommand

      Dim cmdText As String
      cmdText = "IF EXISTS (SELECT * FROM   INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = 'RAVI' AND TABLE_NAME = '" & paper & "') "
      cmdText = cmdText & " BEGIN"
      cmdText = cmdText & " INSERT INTO " & paper & "(question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
      cmdText = cmdText & " END"
      cmdText = cmdText & " ELSE"
      cmdText = cmdText & " BEGIN"
      cmdText = cmdText & " CREATE TABLE " & paper & "(question_Id int AUTO_INCREMENT,question varchar(MAX),option1 varchar(255),option2 varchar(255),option3 varchar(255),option4 varchar(255),answer varchar(2));"
      cmdText = cmdText & " INSERT INTO " & paper & "(question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
      cmdText = cmdText & " END"
      Try
          cmd.CommandText = cmdText.ToString
          cmd.Connection = cn
          cmd.Connection.Open()
          cmd.ExecuteNonQuery()
          cmd.Connection.Close()
          cmd.Dispose()
          cn.Dispose()
          Response.Write("Question Added !!")
      Catch ex As Exception
          Response.Write(ex.ToString)
      End Try

但是,我得到了异常

  

System.Data.SqlClient.SqlException:&#39; - &#39;附近的语法不正确。 &#39; - &#39;附近的语法不正确。 &#39;附近的语法不正确 - &#39;。
  在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,   Boolean breakConnection)
  在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException   exception,Boolean breakConnection)
  在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject)   stateObj)
  在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,   BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject   stateObj)
  在System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String   methodName,Boolean async)
  在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult   result,String methodName,Boolean sendToPipe)
  在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
  在ASP.admin_addquestion_aspx ._ 渲染 _control1(HtmlTextWriter __w,   控制参数容器)   D:\ EWA \ eExam \ admin \ addQuestion.aspx:第40行

3 个答案:

答案 0 :(得分:1)

请使用

    Dim cmdText As String
    cmdText = "IF EXISTS (SELECT * FROM   INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = 'RAVI' AND TABLE_NAME = '" & paper & "') "
    cmdText = cmdText & " BEGIN"
    cmdText = cmdText & " INSERT INTO [" & paper & "](question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
    cmdText = cmdText & " END"
    cmdText = cmdText & " ELSE"
    cmdText = cmdText & " BEGIN"
    cmdText = cmdText & " CREATE TABLE [" & paper & "](question_Id int IDENTITY(1,1),question varchar(MAX),option1 varchar(255),option2 varchar(255),option3 varchar(255),option4 varchar(255),answer varchar(2));"
    cmdText = cmdText & " INSERT INTO [" & paper & "](question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
    cmdText = cmdText & " END"

而不是

   Dim cmdText As String
  cmdText = "IF EXISTS (SELECT * FROM   INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = 'RAVI' AND TABLE_NAME = '" & paper & "') "
  cmdText = cmdText & " BEGIN"
  cmdText = cmdText & " INSERT INTO " & paper & "(question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
  cmdText = cmdText & " END"
  cmdText = cmdText & " ELSE"
  cmdText = cmdText & " BEGIN"
  cmdText = cmdText & " CREATE TABLE " & paper & "(question_Id int AUTO_INCREMENT,question varchar(MAX),option1 varchar(255),option2 varchar(255),option3 varchar(255),option4 varchar(255),answer varchar(2));"
  cmdText = cmdText & " INSERT INTO " & paper & "(question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
  cmdText = cmdText & " END"

问题是:

1在创建和插入时,表名应为[]。 2对于autoincremnet,SQL Server中将有IDENTITY(1,1)。

答案 1 :(得分:0)

您可以尝试在表名周围添加[],这样就可以了;

"IF EXISTS (SELECT * FROM   INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = 'RAVI' AND TABLE_NAME = ['" & paper & "']) "

答案 2 :(得分:0)

表名中不允许使用连字符“ - ”。您必须用下划线“_”替换它或完全删除它。