how to use table type in .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;
            }

        }
时间: 2024-08-29 00:16:29

how to use table type in .net的相关文章

Create User-Defined Table Type

使用 Create type 创建自定义的Table Type ,Syntax 如下,和Create Table的 语法十分相似. CREATE TYPE [ schema_name. ] type_name { FROM base_type [ ( precision [ , scale ] ) ] [ NULL | NOT NULL ] | AS TABLE ( { <column_definition> | <computed_column_definition> } [ &

[Err] 1214 - The used table type doesn&#39;t support FULLTEXT indexes

-- -- Table structure for table `film_text` -- -- InnoDB added FULLTEXT support in 5.6.10. If you use an -- earlier version, then consider upgrading (recommended) or -- changing InnoDB to MyISAM as the film_text engine -- CREATE TABLE film_text ( fil

MySQL explain type详解

对于MySQL执行计划的获取,我们可以通过explain方式来查看,explain方式看似简单,实际上包含的内容很多,尤其是输出结果中的type类型列.理解这些不同的类型,对于我们SQL优化举足轻重. 一.EXPLAIN 语句中type列的值 类型 含义 system 表只有一行 const 表最多只有一行匹配,通用用于主键或者唯一索引比较时 eq_ref 每次与之前的表合并行都只在该表读取一行,这是除了system,const之外最好的一种,特点是使用=,而且索引的所有部分都参与join且索引

MySQL 派生表(Derived Table) Merge Optimization

本文将通过演示告诉你:MySQL中派生表(Derived Table)是什么?以及MySQL对它的优化. Background 有如下一张表: mysql> desc city; +------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+-

mysql explain 中type的归纳

为了更好的理解连接类型(type),将根据查询条件的不同对连接类型进行简单归纳. 表定义如下: 1.id为主键 mysql> show create table key_id; +--------+-------------------------------------------------------------------------------------------------------------------------------------------------------

MySQL之explain 的type列 &amp; Extra列

explain 可以分析 select 语句的执行,即 MySQL 的“执行计划. 一.type 列 MySQL 在表里找到所需行的方式.包括(由左至右,由最差到最好): | All | index | range | ref | eq_ref | const,system | null | ALL(所有) 全表扫描,MySQL 从头到尾扫描整张表查找行. mysql> explain select * from a\G ...         type: ALL 如果加上 limit 如 se

MySQL 5.6 Reference Manual-14.6 InnoDB Table Management

14.6 InnoDB Table Management 14.6.1 Creating InnoDB Tables 14.6.2 Moving or Copying InnoDB Tables to Another Machine 14.6.3 Grouping DML Operations with Transactions 14.6.4 Converting Tables from MyISAM to InnoDB 14.6.5 AUTO_INCREMENT Handling in Inn

Mysql –&gt;EF edmx(model first)–&gt; Sql server table

一.mysql environment When we create an new database,first We need draw er diagram for somebody to show your idea,but our company have no good authorised tool to design sqlserver ER diagram,so I use mysql graphical tool to design it, after that,you can

如何在Lua与C/C++之间实现table数据的交换

之前在<C/C++和Lua是如何进行通信的?>一文中简单的介绍了lua与宿主之间的通信.简单的说两种不同的语言之间数据类型不一样又如何进行数据交换呢?那就是lua_State虚拟栈,通过栈操作和lua库函数,我们很轻松就能完成两者之间的数据交换. 开始之前,明确几个问题,lua中的虚拟栈的索引编号问题(我们假设栈大小为n),编号1是栈底,n视栈顶,编号-1是栈顶,-n是栈底.lua中的库函数需要访问和操作栈上的数据都是通过索引编号定位的.但是我们需要明确一点,有些API并没有使用索引编号作为参