【问题标题】:missing semicolon at end of sql statementsql语句末尾缺少分号
【发布时间】:2012-05-23 05:45:37
【问题描述】:

当我朗读我的代码时出现问题,然后出现错误“在 sql 语句末尾缺少分号”。

我的代码是:

代码

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        try
        {
            FileUpload img = (FileUpload)imgUpload;
            Byte[] imgByte = null;
            if (img.HasFile && img.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = imgUpload.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
            }

            string str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Geeta/Desktop/mssl2.accdb;Persist Security Info=False";);


            OleDbConnection conn = new OleDbConnection(str);

            conn.Open();
            string sql = "INSERT INTO digital(Product_Name, Product_Code, Product_Price, Product_Image, Product_Description) VALUES(@pnm, @pcod, @ppr, @pimg, @pdes) SELECT @@IDENTITY;";
            OleDbCommand cmd = new OleDbCommand(sql, conn);

            cmd.Parameters.AddWithValue("@pnm", txtEName.Text.Trim());
            cmd.Parameters.AddWithValue("@pcod", txt_productcode.Text.Trim());
            cmd.Parameters.AddWithValue("@ppr", txt_productprice.Text.Trim());
            cmd.Parameters.AddWithValue("@pdes", txt_productdescri.Text.Trim());
            cmd.Parameters.AddWithValue("@pimg", imgByte);
            int Id = Convert.ToInt32(cmd.ExecuteScalar());
            lblResult.Text = String.Format("Employee ID is {0}", Id);
            conn.Close();

        }

        catch
        {
            lblResult.Text = "There was an error";
        }
        finally
        {

        }
    } 
}

【问题讨论】:

标签: c#-4.0 ms-access-2007


【解决方案1】:

在选择@@identity 之前添加分号。然后尝试执行。这意味着插入语句一个分号和分号上的选择。

【讨论】:

  • 嗨,当我在插入和选择语句之间放置分号时,我在 SQL 语句结束后发现错误字符。
  • 正如我在您之前的帖子中所说,您不能使用 MS Access 执行两条语句。
【解决方案2】:

在“SELECT @@IDENTITY”之前,您需要一个分号。 从技术上讲,您正在创建两个 SQL 语句。 一个用于插入,一个用于 SELECT @@IDENTITY。这就是为什么您需要在这两个查询之间使用分号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2019-04-18
    相关资源
    最近更新 更多