简体   繁体   English

猜猜插入语句中的错误

[英]Guess the error in the insert statement

Anybody out there with some eagle eyes? 那里有一些鹰眼的人吗? I want to fish out the location of the error in the following insert statement. 我想在下面的插入语句中删除错误的位置。 i used a breakpoint to track the codes while running and i get the exception error that says (There was an Incorrect syntax near the keyword 'Case'). 我在运行时使用了断点来跟踪代码,并且我得到了异常错误(在关键字“Case”附近有一个不正确的语法)。 Get you eagles eyes on... 让你的老鹰眼睛......

public partial class OpenCase : System.Web.UI.Page
{
    string adminString;
    protected void Page_Load(object sender, EventArgs e)
    {
        adminString = "CA123";
    }

    protected void openCaseBotton_Click(object sender, EventArgs e)
    {
        //SQL connection string
        SqlDataSource CSMDataSource = new SqlDataSource();
        CSMDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ToString();

        //SQL Insert command with variables
        CSMDataSource.InsertCommandType = SqlDataSourceCommandType.Text;
        CSMDataSource.InsertCommand = "INSERT INTO Case (CaseID, CaseDesc, DateOpened, CasePriority, AdministratorID) VALUES (@CaseID, @CaseDesc, @DateOpened, @CasePriority, @AdministratorID)";

        //Actual Insertion with values from textboxes into databse fields
        CSMDataSource.InsertParameters.Add("CaseID", caseIDTextBox.Text);
        CSMDataSource.InsertParameters.Add("CaseDesc", caseDescTextBox.Text);
        CSMDataSource.InsertParameters.Add("DateOpened", DateTime.Now.ToString());
        CSMDataSource.InsertParameters.Add("CasePriority", null);
        CSMDataSource.InsertParameters.Add("AdministratorID", adminString.ToString());

        int rowsCommitted = 0;

        //Try catch method to catch exceptions during insert
        try
        {
            rowsCommitted = CSMDataSource.Insert();
            Response.Write(@"<script language='javascript'>alert('The following errors have occurred:');</script>");
        }

        catch (Exception ex)
        {
            //error message displayed when exception occurs


            string script = "<script>alert('" + ex.Message + "');</script>";
            Response.Write("The following Error occurred while entering the records into the database" + " " + ex.ToString() + " ");
            Response.Redirect("~/ErrorPage.aspx", false);
        }
        finally
        {
            CSMDataSource = null;
        }

        //Where to go next if insert was successful or failed
        if (rowsCommitted != 0)
        {
            Response.Redirect("~/CaseAdmin.aspx", false);
        }
        else
        {

            Response.Redirect("~/ErrorPage.aspx", false);
        }

    }

    protected void addExhibitBotton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/EntryForms/AddExhibit.aspx", false);

    }
}

Seen anything? 看到什么? may be u need a C# error vision goggles...lol 可能你需要一个C#错误视力护目镜......哈哈

Near the INSERT ? INSERT附近? well, CASE is a SQL reserved word, so try simply: 好吧, CASE是一个SQL保留字,所以试试吧:

CSMDataSource.InsertCommand = "INSERT INTO [Case] (...

to tell it that Case is an object name, not the SQL keyword. 告诉它Case是一个对象名,而不是SQL关键字。

I'm pretty certain that case is actually an SQL keyword. 我很确定这种case实际上是一个SQL关键字。

Depending on the DBMS, you may have to do something like enclose it in backticks or other delimiters to tell the execution engine to treat it as a non-keyword, something like: 根据DBMS,您可能需要做一些事情,例如将其括在反引号或其他分隔符中,以告诉执行引擎将其视为非关键字,例如:

CSMDataSource.InsertCommand = "INSERT INTO `Case` (CaseID, ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM