您可以使用带有单词的文本框并将其传递给SQL存储过程?我想使用存储过程,但我试图创建一个搜索页面,所以我想我正在试图找出如何保留存储过程,但将文本框的值传递给存储过程。SQL存储过程值来自页面
1
A
回答
3
是的,很容易,这里是一个完整的article举例
下面是相关的代码片段:
类别名称可以从你的控件的Text属性进行设置。
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();
}
}
+0
值得一提的是,有些东西(称为它们 - 工具?库?框架?)可以让这个过程更容易一些,例如, Linq To SQL,Enterprise Library,nHibernate,Entity Framework等 – 2011-03-03 17:53:16
+0
当然,我刚刚认为OP是一个比较新的东西,俗话说他必须飞行的人必须先学会走路等,即学习大多数抽象的东西,然后抽象出 – kd7 2011-03-03 17:55:49
0
是的,你可以做到这一点。看看SQL Parameters。
相关问题
- 1. MS SQL存储过程来获取值
- 2. 存储过程的价值asp.net页面
- 3. 插入SQL存储过程值到另一个存储过程
- 4. 来自SQL语句的SQL Server存储过程生成器?
- 5. 将asp页面的值发送到SQL存储过程并返回值?
- 6. SQL Server存储过程来了空
- 7. SQL存储过程
- 8. 存储过程/ SQL
- 9. SQL存储过程
- 10. SQL存储过程
- 11. SQL存储过程
- 12. SQL存储过程
- 13. 结果Column Null来自存储过程
- 14. 来自Ajax的更新存储过程
- 15. 获取来自Oracle存储过程
- 16. 来自同一个存储过程
- 17. SQL存储过程自动运行
- 18. VBA调用SQL存储过程导致其他页面挂起
- 19. 获取页面SQL搜索存储过程的计数
- 20. 在SQL Server 2008 R2中存储来自存储过程的多个数据集?
- 21. SQL存储过程“默认参数值”
- 22. 在SQL存储过程中连接值
- 23. 存储过程NULL值SQL Server
- 24. SQL-CLR存储过程不返回值
- 25. SQL Server存储过程的返回值
- 26. SQL存储过程比较值
- 27. 存储过程接受可选值SQL
- 28. SQL存储过程无法返回值
- 29. SQL:在存储过程中显示来自表的结果
- 30. 来自扩展存储过程的SQL Server捕获错误
一些更多的细节会很好。语言? WinForm的/ Web窗体? – 2011-03-03 16:18:07