C# &引用;必须声明标量变量“;错误
必须声明标量变量“@Id”C# &引用;必须声明标量变量“;错误,c#,sql-server,C#,Sql Server,必须声明标量变量“@Id” 有人能帮我吗 因为您在SqlCommand中声明了Id参数,但没有添加任何值 在属性中分配文本时,不需要使用() cmd.CommandText = "SELECT Goal from Planning WHERE Id = @Id"; cmd.Parameters.AddWithValue("@Id", YourIdValue); int goal = (int)cmd.ExecuteScalar(); 还可用于处理SqlConnection和SqlCommand
有人能帮我吗 因为您在
SqlCommand
中声明了Id
参数,但没有添加任何值
在属性中分配文本时,不需要使用()
cmd.CommandText = "SELECT Goal from Planning WHERE Id = @Id";
cmd.Parameters.AddWithValue("@Id", YourIdValue);
int goal = (int)cmd.ExecuteScalar();
还可用于处理SqlConnection
和SqlCommand
这里有一个完整的例子
using(SqlConnection con = new SqlConnection(connString))
using(SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "SELECT Goal from Planning WHERE Id = @Id";
cmd.Parameters.AddWithValue("@Id", YourIdValue);
try
{
conn.Open();
int goal = (int)cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
您应该为参数指定一个值
cmd.Parameters.AddWithValue("@Id", value);
第一行是您的错误消息吗?是的,这是我的第一条错误消息。顺便说一句,您的连接字符串似乎也错了。如果您的
connectionstring
是一个变量,您应该像newsqlconnection(connectionstring)
一样使用它,而不是newsqlconnection(@“connectionstring”)
。
cmd.Parameters.AddWithValue("@Id", value);