gpt4 book ai didi

c# - 无效的列名 SQL Server

转载 作者:行者123 更新时间:2023-11-30 18:49:01 25 4
gpt4 key购买 nike

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

"invalid column name" for all of the eventspecialist, phone, phon2, etc. and @SPECIALIST, @CUST_PHONE, @CUST_PHONE2, etc.

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

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

"Invalid column name eventspecialist, Invalid column name phone, Invalid column name phone2, etc."

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;
}
}

最佳答案

先列名后变量

string sql = "INSERT INTO tblcontacts (Col1,Col2,...) VALUES (@Var1,@Var2,...)"

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com