What is the simplest way to store a string into variable. For example
How do I store something into @bleh variable.
SqlCommand stej = new SqlC开发者_运维知识库ommand("SELECT COUNT(*) FROM test3 WHERE PostID = @bleh", con);
@bleh variable has to be set somehow. I tried replacing @bleh with a 100,abc,da, etc. And it works. But my bleh variable will change with the loop.
Thanks
These are called Parameters.
To set the value:
stej.Parameters.AddWithValue("@bleh", postId);
(There are more options, if you need to explicitly control the datatype, etc)
A single Parameter can only contain one value though. If you wish to pass multiple values, you would need multiple parameters.
Use a sql parameter
using ( SqlConnection conn = new SqlConnection( "connection string" )
{
conn.Open();
string selstr = "SELECT COUNT(*) FROM test3 WHERE PostID = @bleh";
SqlCommand cmd = new SqlCommand( selstr, conn );
SqlParameter name = cmd.Parameters.Add( "@bleh", SqlDbType.NVarChar, 255 );
name.Value = "value";
int count = cmd.ExecuteScalar();
//Do you stuff
}
精彩评论