每当我提交表格时,都会给我一个错误代码:


  所有事件的“无效列名”:pecialist,phone,phon2等,以及@ SPECIALIST,@ CUST_PHONE,@ CUST_PHONE2等。


这是下面的代码,也许我没有正确编写SQL语句或参数?具有@infront的是我数据库中的内容,其他小写的是我的文本框。对于这种编码,我还比较陌生。

编辑:将我的INSERT INTO语句更改为建议。该错误仍然存​​在,但仅被最小化为


  “无效的列名称事件专科医生,无效的列名称电话,无效的列名称phone2等。”


private void execution(string eventspecialist, string phone, string phone2, string firstname, string lastname, string besttime, string companyname, string nonprofit, string requesteddate, string requestedtime, string attendance, string eventtype, string other, string leadsource, string notes, string catering, string bar, string damagedeposit, string dancefloor, string griddate, string gridnotes, string comments)
{
    SqlConnection conn = new SqlConnection(GetConnectionString());

    string sql = "INSERT INTO tblcontacts (@SPECIALIST, @CUST_PHONE1, @CUST_PHONE2, @CUST_FNAME, @CUST_LNAME, @BEST_TIME, @COMPANY_NAME, @NONPROFIT, @REQ_DATE, @REQ_TIME, @ATTENDANCE, @EVENT_TYPE, @OTHER_DESC, @LEAD_SOURCE, @NOTES, @CATERING, @BAR, @DAMAGE_DEPOSIT, @DANCE_FLOOR) VALUES (eventspecialist, phone, phone2, firstname, lastname, besttime, companyname, nonprofit, requesteddate, requestedtime, attendance, eventtype, other, leadsource, notes, catering, bar, damagedeposit, dancefloor)";
    string sql2 = "INSERT INTO tblnotes (@NOTEDATE, @NOTEBY, @COMMENTS) VALUES (griddate, gridnotes, comments)";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);

        cmd.Parameters.Add("@SPECIALIST", SqlDbType.NVarChar, 50).Value = eventspecialist;
        cmd.Parameters.Add("@CUST_PHONE1", SqlDbType.NVarChar, 50).Value = phone;
        cmd.Parameters.Add("@CUST_PHONE2", SqlDbType.NVarChar, 50).Value = phone2;
        cmd.Parameters.Add("@CUST_FNAME", SqlDbType.NVarChar, 50).Value = firstname;
        cmd.Parameters.Add("@CUST_LNAME", SqlDbType.NVarChar, 50).Value = lastname;
        cmd.Parameters.Add("@BEST_TIME", SqlDbType.NVarChar, 50).Value = besttime;
        cmd.Parameters.Add("@COMPANY_NAME", SqlDbType.NVarChar, 225).Value = companyname;
        cmd.Parameters.Add("@NONPROFIT", SqlDbType.NVarChar, 10).Value = nonprofit;
        cmd.Parameters.Add("@REQ_DATE", SqlDbType.Date, 20).Value = requesteddate;
        cmd.Parameters.Add("@REQ_TIME", SqlDbType.Time, 20).Value = requestedtime;
        cmd.Parameters.Add("@ATTENDANCE", SqlDbType.Int, 50).Value = attendance;
        cmd.Parameters.Add("@EVENT_TYPE", SqlDbType.NVarChar, 50).Value = eventtype;
        cmd.Parameters.Add("@OTHER_DESC", SqlDbType.NVarChar, 225).Value = other;
        cmd.Parameters.Add("@LEAD_SOURCE", SqlDbType.NVarChar, 50).Value = leadsource;
        cmd.Parameters.Add("@NOTES", SqlDbType.NVarChar, 225).Value = notes;
        cmd.Parameters.Add("@CATERING", SqlDbType.NVarChar, 1).Value = catering;
        cmd.Parameters.Add("@BAR", SqlDbType.NVarChar, 1).Value = bar;
        cmd.Parameters.Add("@DAMAGE_DEPOSIT", SqlDbType.NVarChar, 19).Value = damagedeposit;
        cmd.Parameters.Add("@DANCE_FLOOR", SqlDbType.Money).Value = Decimal.Parse(dancefloor);


        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex_msg)
    {
        string msg = "Error occured while inserting";
        msg += ex_msg.Message;
        throw new Exception(msg);
    }
    finally
    {

        conn.Close();
    }
    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql2, conn);

        cmd.Parameters.Add("@NOTEDATE", SqlDbType.Date, 50).Value = griddate;
        cmd.Parameters.Add("@NOTEBY", SqlDbType.NVarChar, 50).Value = gridnotes;
        cmd.Parameters.Add("@COMMENTS", SqlDbType.NVarChar, 50).Value = comments;

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex_msg)
    {
        string msg = "Error occured while inserting";
        msg += ex_msg.Message;
        throw new Exception(msg);
    }
    finally
    {

        conn.Close();
    }

}
protected void Page_Load(object sender, EventArgs e)
{
    griddate.Text = DateTime.Now.ToString("yyyy/MM/dd");
}
protected void submit_Click(object sender, EventArgs e)
{
    if (requesteddate.Text == "")
    {
        Finish.Text = "Please complete the form!";
    }
    else if (requestedtime.Text == "")
    {
        Finish.Text = "Please complete the form!";
    }
    else if (attendance.Text == "")
    {
        Finish.Text = "Please complete the form!";
    }
    else
    {
        execution(eventspecialist.Text, phone.Text, phone2.Text, firstname.Text, lastname.Text, besttime.SelectedItem.Text, companyname.Text, nonprofit.Text, requesteddate.Text, requestedtime.Text, attendance.Text, eventtype.SelectedItem.Text, other.Text, leadsource.SelectedItem.Text, notes.Text, catering.Text, bar.Text, damagedeposit.Text, dancefloor.SelectedItem.Text, griddate.Text, gridnotes.SelectedItem.Text, comments.Text);
        Finish.Visible = false;
        conform.Visible = true;
    }
}

最佳答案

我想你的专栏是

SPECIALIST, CUST_PHONE1, CUST_PHONE2, CUST_FNAME, CUST_LNAME, BEST_TIME,
COMPANY_NAME, NONPROFIT, REQ_DATE, REQ_TIME, ATTENDANCE, EVENT_TYPE, OTHER_DESC,
LEAD_SOURCE, NOTES, CATERING, BAR, DAMAGE_DEPOSIT, DANCE_FLOOR


如果是这样,您应该以这种方式更改INSERT INTO

INSERT INTO tblcontacts
(SPECIALIST, CUST_PHONE1, CUST_PHONE2, CUST_FNAME, CUST_LNAME, BEST_TIME,
COMPANY_NAME, NONPROFIT, REQ_DATE, REQ_TIME, ATTENDANCE, EVENT_TYPE, OTHER_DESC, LEAD_SOURCE,
NOTES, CATERING, BAR, DAMAGE_DEPOSIT, DANCE_FLOOR)
VALUES
(@SPECIALIST, @CUST_PHONE1, @CUST_PHONE2, @CUST_FNAME, @CUST_LNAME, @BEST_TIME, @COMPANY_NAME,
 @NONPROFIT, @REQ_DATE, @REQ_TIME, @ATTENDANCE, @EVENT_TYPE, @OTHER_DESC, @LEAD_SOURCE, @NOTES,
 @CATERING, @BAR, @DAMAGE_DEPOSIT, @DANCE_FLOOR)


execution方法中,您正在创建参数,但是随后在insert语句中
首先输入真实的列名称,然后在值部分中输入参数名称
(带有@prefix)

作为旁注,下一次,请尝试格式化更好的代码。

关于c# - 无效的列名SQL Server,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9635224/

10-13 07:09