必须声明标量变量"@connection"错误 [英] Must declare the scalar variable '@connection' error
问题描述
下面是要获取的代码
必须声明标量变量@connection
must declare scalar variable @connection
错误.我不知道我要去哪里错了.请指导
error. I don't know where I am going wrong. Please guide
protected void LinkButton1_Click(object sender, EventArgs e)
{
string connection = Drpconn.SelectedItem.Text;
using (OdbcConnection con = new OdbcConnection("DSN=Sqltesting;UID=user1;PWD=test@123;Integrated Security=no;"))
{
using (OdbcCommand cmd = new OdbcCommand("INSERT INTO TblConfigure(Connection,Server,DbName,UserID,Password,Connection_Name,Port,Service_ID) VALUES (@Connection, @Server , @DbName,@UserID,@Password,@ConnectionName,@Port,@ServiceID)", con))
{
con.Open();
cmd.Parameters.AddWithValue("@Connection", connection);
cmd.Parameters.AddWithValue("@Server", TxtServer.Text);
cmd.Parameters.AddWithValue("@DbName", DrpDbName.SelectedItem.Text);
cmd.Parameters.AddWithValue("@UserID", TxtUsr.Text);
cmd.Parameters.AddWithValue("@Password", TxtPass.Text);
cmd.Parameters.AddWithValue("@ConnectionName", Txtconnname.Text);
cmd.Parameters.AddWithValue("@Port", TxtPort.Text);
cmd.Parameters.AddWithValue("@ServiceID", TxtService.Text);
cmd.ExecuteNonQuery();
}
} // closes the connection
Response.Redirect("LoginPL.aspx");
}
推荐答案
You need to rewrite your command text to follow the guidelines for ODBC parameters. With this provider you cannot supply the command text with embedded NAMED placeholders for your parameters.
You provide this text with just a question mark for the parameter.
当CommandType设置为Text时,.NET Framework数据提供程序用于 ODBC不支持将命名参数传递给SQL语句或 到由OdbcCommand调用的存储过程.在任何一个 情况下,请使用问号(?)占位符
When CommandType is set to Text, the .NET Framework Data Provider for ODBC does not support passing named parameters to an SQL statement or to a stored procedure called by an OdbcCommand. In either of these cases, use the question mark (?) placeholder
此外,当您将参数添加到命令参数"集合中时,还应按照INSERT字段期望的确切顺序提供它们. (但这在您当前的代码中已经是正确的)
Also when you add the parameters to the command Parameters collection you should provide them in the exact order expected by the INSERT fields. (But this is already correct in your current code)
string cmdText = @"INTO TblConfigure
(Connection,Server,DbName,UserID,
Password,Connection_Name,Port,Service_ID)
VALUES (?,?,?,?,?,?,?,?)";
using (OdbcCommand cmd = new OdbcCommand(cmdText, con))
{
con.Open();
cmd.Parameters.AddWithValue("@Connection", connection);
.....
最后的笔记.当心AddWithValue.这是一个方便的快捷方式,但在某些情况下会伤到您.请参见我们可以停止使用AddWithValueAlready?
A final note. Beware of AddWithValue. It is an handy shortcut, but in certain circumstances it bites you. See Can we stop using AddWithValueAlready?
这篇关于必须声明标量变量"@connection"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!