C # Call custom table type parameters

Source: Internet
Author: User

C # Call custom table type parameters

-Test environment generated by SQL SERVER:

 

-- CREATE test DBCREATE database Sales; goUSE SalesGO -- CREATE the table type if TYPE_ID ('localdt ') is not nulldrop type LocalDTGO create type LocalDT as table (id int not null, name NVARCHAR (50) GO -- create the stored procedure if OBJECT_ID ('P _ able ', 'P') is not nulldrop proc P_DataTable; gocreate procedure P_DataTable (@ LocalDT READONLY) ASSELECT * FROM @ LocalDTGO

-- Open Visual Studio-create a project-select console application]

 

 

Using System; using System. collections. generic; using System. linq; using System. text; using System. data; using System. data. sqlClient; namespace ProcDataTable {class Program {static void Main (string [] args) {DataTable dt = new DataTable ("LocalDT"); dt. columns. add ("ID", typeof (int); dt. columns. add ("Name", typeof (string); DataRow dr = dt. newRow (); dr [0] = 1; dr [1] = "Roy"; dt. rows. add (dr); SqlConnection thisConnection = new SqlConnection (@ "Server = Instance name; Database = Sales; User ID = sa; Password = 1"); thisConnection. open (); SqlCommand sqlcmd = thisConnection. createCommand (); sqlcmd. commandType = CommandType. storedProcedure; sqlcmd. commandText = "P_DataTable"; SqlParameter param = sqlcmd. parameters. addWithValue ("@ LocalDT", dt); SqlDataReader sdr = sqlcmd. executeReader (); while (sdr. read () {Console. writeLine ("ID: {0} \ tName: {1}", sdr [0], sdr [1]);} thisConnection. close (); Console. readKey ();}}}

-- Test Result:

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.