你有一个带有单词的文本框并将其传递给SQL存储过程吗?我想使用存储过程,但我想创建一个搜索页面,所以我想我试图找出如何保存存储过程,但将文本框的值传递给存储过程。
答案 0 :(得分:3)
是的,非常容易,这是一个完整的article示例
以下是相关的代码段:
可以从控件的文本属性中设置类别名称。
static void GetSalesByCategory(string connectionString,
string categoryName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SalesByCategory";
command.CommandType = CommandType.StoredProcedure;
// Add the input parameter and set its properties.
// HERE IS What you need.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;
// Add the parameter to the Parameters collection.
command.Parameters.Add(parameter);
// Open the connection and execute the reader.
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}
答案 1 :(得分:0)
是的,你可以这样做。查看SQL Parameters。