Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of passing parameters of SQL Server 2008 table type under C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the method of passing SQL Server 2008 table type parameters under C#". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of passing SQL Server 2008 table type parameters under C#"?

We demonstrate the new features of SQL Server 2008 through ADO.Net under C#.

First, we create a sample database named Test under SQL Server 2008, and then create a table called User under that database, with the following structure:

Then create a custom table type under the Test database and name it UserDetailType, as follows:

CREATE TYPE [dbo]. [UserDetailsType] AS TABLE ([ID] [varchar] (50) NULL, [Name] [varchar] (50) NULL, [Sex] [varchar] (50) NULL, [Age] [decimal] (18,0) NULL)

Then, create a stored procedure named InsertUserInfo, as follows:

CREATE PROCEDURE [dbo]. [InsertUserInfo] @ UserInfo [UserDetailsType] readonly AS BEGIN insert into [User] ([ID], [Name], [Sex], [Age]) select [ID], [Name], [Sex], [Age] from @ UserInfo; END

After starting Visual Studio 2008 and creating a default forms application, we need to create an instance of the database table DataTable in memory, as follows:

Private static DataTable PrepareDatatable () {DataTable dt = new DataTable ("dt"); DataColumn [] dtc = new DataColumn [4]; dtc [0] = new DataColumn ("ID", System.Type.GetType ("System.String")); dtc [1] = new DataColumn ("Name", System.Type.GetType ("System.String")); dtc [2] = new DataColumn ("Sex", System.Type.GetType ("System.String")) Dtc [3] = new DataColumn ("Age", System.Type.GetType ("System.Decimal")); dt.Columns.AddRange (dtc); return dt;}

Then, execute the Test database stored procedure InsertUserInfo that we just created through SqlCommand, and pass the instance of DataTable that we created in memory, as follows:

Private static void SaveUserInfoDetails () {DataTable dt = PrepareDatatable (); for (int item0 +) {DataRow dr = dt.NewRow (); dr [0] = i.ToString (); dr [1] = "Name" + i.ToString (); dr [2] = "male" Dr [3] = (iTun10) .ToString (); dt.Rows.Add (dr);} using (SqlConnection conn = new SqlConnection ("server=Rithia;database=Test;integrated security=SSPI")) {SqlCommand cmd = conn.CreateCommand (); cmd.CommandType = System.Data.CommandType.StoredProcedure Cmd.CommandText = "dbo.InsertUserInfo"; SqlParameter param = cmd.Parameters.AddWithValue ("@ UserInfo", dt); conn.Open (); cmd.ExecuteNonQuery ();}}

Through the above example, we can first create the table type data to be passed on the program client, and then pass it to the stored procedure, while the stored procedure adds the records from the SQL Server 2008 table type parameters to the database physical table at one time, which is very convenient when you need to pass to the parameters in the form of an array of stored procedures.

At this point, I believe you have a deeper understanding of "what is the method of passing SQL Server 2008 table type parameters under C#". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report