How to use the table type in. Net

Source: Internet
Author: User

1. Create Table type in Sqlserver2008.

CREATE TYPE   as table (    [Wordtext] [nchar] (+) NULL,    [WordCount] [ int ] NULL)

and the target table is:

  

2.Create Store Procedure.

ALter PROCEDURE [dbo]. [A_words]      as dbo. Wordtable readonlyasbegin    INSERT INTO dbo. A_wordcount (wordcount,wordtext)    Select from @Word w    END

3. First we'll create a table. Then we'll pass this table to the store procedure.

         Public StaticDataTable ConvertToTable (list<wordclass>wordlst) {DataTable dt=NewDataTable (); Dt. Columns.Add ("Wordtext",typeof(string)); Dt. Columns.Add ("Count",typeof(int)); foreach(varWordinchwordlst) {DataRow row=dt.                NewRow (); row["Wordtext"] =Word.                Wordvalue; row["Count"] =Word.                Count; Dt.            Rows.Add (row); }            returnDT; }
         Public Static voidWriteData (DataTable dttable) {CommandText="dbo. [A_words]";SqlConnection con; Try            {                using(Con =NewSqlConnection (connectionString)) {SqlCommand cmd =NewSqlCommand (CommandText, con); Cmd.commandtype=CommandType.StoredProcedure; Cmd. Parameters.Add (NewSqlParameter ("@Word", sqldbtype.structured)); Cmd. parameters["@Word"]. Value =dttable;con.                    Open (); Cmd.                    ExecuteNonQuery (); Con. Close ();                }            }            Catch(Exception) {Throw; }        }

How to use the table type in. Net

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.