zoukankan      html  css  js  c++  java
  • bulk insert data into database with table type .net

    1. Create Table type in Sqlserver2008.

    CREATE TYPE  dbo.WordTable as table
    (
        [WordText] [nchar](100) NULL,
        [WordCount] [int] NULL
    )

    And the target table is:

    CREATE TABLE [dbo].[A_WordCount](
    	[id] [int] IDENTITY(1,1) NOT NULL,
    	[WordText] [nchar](100) NULL,
    	[WordCount] [int] NULL
    ) 
    

      

    2.Create Store Procedure.

    ALter PROCEDURE [dbo].[A_Words]
        @Word as dbo.WordTable READONLY
    AS
    
    BEGIN
        insert into dbo.A_WordCount(WordCount,WordText)
        select w.WordCount,w.WordText from @Word w
        
    END

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

            public static DataTable ConvertToTable(List<WordClass> wordLst)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("WordText", typeof (string));
                dt.Columns.Add("Count", typeof (int));
    
                foreach (var word in wordLst)
                {
                    DataRow row = dt.NewRow();
                    row["WordText"] = word.WordValue;
                    row["Count"] = word.Count;
                    dt.Rows.Add(row);
                }
                return dt;
            }
            public static void WriteData(DataTable dtTable)
            {
                commandText = "dbo.[A_Words]";
                SqlConnection con;
                try
                {
                    using (con = new SqlConnection(connectionString))
                    {
                        SqlCommand cmd = new SqlCommand(commandText, con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Word", SqlDbType.Structured));
                        cmd.Parameters["@Word"].Value = dtTable;
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
    
            }
  • 相关阅读:
    canvas背景粒子动态变化动画
    点击屏幕弹出心形效果
    前端图片的性能优化
    vue的computed和method的区别
    es6的...
    命名路由和命名视图
    编程式路由
    [思维]蚂蚁感冒
    [模板]前缀树 / 字典树及应用
    [模板]三分搜索
  • 原文地址:https://www.cnblogs.com/bg57/p/3963973.html
Copyright © 2011-2022 走看看