如何使用ADO.NET参数

发布时间:2021-11-03 15:08:09 作者:小新
来源:亿速云 阅读:155

这篇文章将为大家详细讲解有关如何使用ADO.NET参数,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

在数据驱动的应用程序中,存储过程具有许多优势。通过利用存储过程,数据库操作可以封装在单个命令中,为获取***性能而进行优化并通过附加的安全性得到增强。尽管可以通过在 SQL 语句中传递后接参数自变量的存储过程名称来调用相应的存储过程,但如果使用 ADO.NET DbCommand 对象的 Parameters 集合,则可以让您更为明确地定义存储过程参数,并访问输出参数和返回值。

使用ADO.NET参数化语句在服务器上通过使用 sp_executesql 执行,sp_executesql 允许重复使用查询计划。sp_executesql 批处理命令中的本地光标或变量对于调用 sp_executesql 的批处理命令是不可见的。数据库上下文中的更改只持续到 sp_executesql 语句的结尾。

对 SqlCommand 使用参数以执行 SQL Server 存储过程时,添加到 Parameters 集合中的参数的名称必须与存储过程中参数标记的名称相匹配。SQL Server 的 .NET Framework 数据访问接口不支持问号 (?)使用ADO.NET参数传递到 SQL 语句或存储过程的占位符。它将存储过程中的参数视为命名参数,并搜索匹配的参数标记。例如,通过使用名为 @CustomerID 的参数定义 CustOrderHist 存储过程。您的代码在执行该存储过程时,它也必须使用名为 @CustomerID 的参数。

此示例演示了如何调用 Northwind 示例数据库中的 SQL Server 存储过程。存储过程的名称为 dbo.SalesByCategory,它具有名为 @CategoryName 的输入参数,其数据类型为 nvarchar(15)。该代码在 using 代码块内创建一个新 SqlConnection,以便在过程结束时释放连接。会创建 SqlCommand 和 SqlParameter 对象,并设置其属性。SqlDataReader 会执行 SqlCommand 并从存储过程返回结果集,以在控制台窗口中显示相关输出。

您可以选择使用任一重载构造函数在一个语句中设置多个属性,而不是创建 SqlCommand 和 SqlParameter 对象,然后在各个语句中设置属性。

Visual Basic

Shared Sub GetSalesByCategory(ByVal connectionString As String, _  ByVal categoryName As String)   Using connection As New SqlConnection(connectionString)   ' Create the command and set its properties.  Dim command As SqlCommand = New SqlCommand()  command.Connection = connection command.CommandText = "SalesByCategory" command.CommandType = CommandType.StoredProcedure   ' Add the input parameter and set its properties.  Dim parameter As 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()  Dim reader As SqlDataReader = command.ExecuteReader()   If reader.HasRows Then  Do While reader.Read()  Console.WriteLine("{0}: {1:C}", _  reader(0), reader(1))  Loop  Else  Console.WriteLine("No rows returned.")  End If  End Using  End Sub

C#

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.  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();  }  }

关于“如何使用ADO.NET参数”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. ADO.NET技术怎么使用
  2. ADO.NET参数相关问题有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ado.net

上一篇:如何看待mysql 5.7 sys数据库表

下一篇:javascript中选择语句有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》