此SqlParameterCollection不包含带有ParameterName的SqlParameter [英] An SqlParameter with ParameterName 'is not contained by this SqlParameterCollection
本文介绍了此SqlParameterCollection不包含带有ParameterName的SqlParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到此代码的错误
此SqlParameterCollection不包含带有ParameterName'@ Main'的SqlParameter。
SqlCommand command = new SqlCommand( < span class =code-string> SELECT * from Variables WHERE ID = @ID,cs);
command.Parameters.Add( @ ID,SqlDbType.Int);
command.Parameters [ @ ID]。Value = Main.pattern3.Text;
cs.Open();
command.ExecuteNonQuery();
cs.Close();
main = command.Parameters [ @ Main]。值。的ToString();
amenu = command.Parameters [ @ Data]。Value.ToString();
amenu1 = command.Parameters [ @ DataDetails]。Value.ToString();
main,amenu和amenu1是字符串
任何人
解决方案
您要显示的示例是尝试使用输出参数。不幸的是,使用select everything查询,这是不可行的。你需要做的是填充一个DataTable并从中获取值。
使用(SqlConnection connection = new SqlConnection(_connectionString))
{
SqlCommand command =
new SqlCommand(SELECT * from Variables WHERE ID = @ID,connection);
command.Parameters.Add(@ ID,SqlDbType.Int);
command.Parameters [@ ID]。Value = Main.pattern3.Text;
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
DataTable dataTable;
dataAdapter.Fill(dataTable);
main =(String)dataTable.Rows [0] [Main];
amenu =(String)dataTable.Rows [0] [Data];
amenu1 =(String)dataTable.Rows [0] [DataDetails];
}
如果你想坚持使用输出参数,你需要将SQL语句切换到存储过程。
I get error on this code
An SqlParameter with ParameterName '@Main' is not contained by this SqlParameterCollection.
SqlCommand command = new SqlCommand("SELECT * from Variables WHERE ID = @ID", cs);
command.Parameters.Add("@ID", SqlDbType.Int);
command.Parameters["@ID"].Value = Main.pattern3.Text;
cs.Open();
command.ExecuteNonQuery();
cs.Close();
main = command.Parameters["@Main"].Value.ToString();
amenu = command.Parameters["@Data"].Value.ToString();
amenu1 = command.Parameters["@DataDetails"].Value.ToString();
main, amenu, and amenu1 are strings
anyone
解决方案
The example you are showing is trying to use Output Parameters. Unfortunately, using a select everything query, this isn't doable. What you need to do is fill a DataTable and get the values out of it.
using(SqlConnection connection = new SqlConnection(_connectionString)) { SqlCommand command = new SqlCommand("SELECT * from Variables WHERE ID = @ID", connection); command.Parameters.Add("@ID", SqlDbType.Int); command.Parameters["@ID"].Value = Main.pattern3.Text; SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataTable dataTable; dataAdapter.Fill(dataTable); main = (String)dataTable.Rows[0]["Main"]; amenu = (String)dataTable.Rows[0]["Data"]; amenu1 = (String)dataTable.Rows[0]["DataDetails"]; }
If you wanted to stick with output parameters, you would need to switch your SQL statement to a stored procedure.
这篇关于此SqlParameterCollection不包含带有ParameterName的SqlParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文