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