问题描述
下面是要获取的代码
错误.我不知道我要去哪里错了.请指导
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");
}
推荐答案
您需要重写命令文本以遵循 ODBC参数.使用此提供程序,您将无法为命令文本提供带有嵌入式NAMED占位符的参数.
您只需为该文本提供参数的问号即可.
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.
此外,当您将参数添加到命令参数"集合中时,还应按照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"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!