栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将数据表从C#传递到SQL Server 2008

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将数据表从C#传递到SQL Server 2008

您缺少a.TypeName =“ dbo.TableTypeInitial”; 将此语句放在“ a.SqlDbType =
SqlDbType.Structured;”之前

使用

cmd.CommandText = "EXEC FinishRegisterChoiceUserInitial @UserId, @TableTypeInitial ";

代替

cmd.CommandText = "EXEC FinishRegisterChoiceUserInitial @UserId, @CurrentTableInitial ";

Sql Server脚本:

         CREATE TABLE [Target]         (         [ID] [int] NOT NULL PRIMARY KEY IDENTITY,         [FirstName] [varchar](100)NOT NULL,         [LastName] [varchar](100)NOT NULL,         [Email] [varchar](200) NOT NULL         )        CREATE TYPE [TargetUDT] AS TABLE         (         [FirstName] [varchar](100)NOT NULL,         [LastName] [varchar](100)NOT NULL,         [Email] [varchar](200) NOT NULL         )         CREATE PROCEDURE AddToTarget(@TargetUDT TargetUDT READONLY)         AS         BEGIN    INSERT INTO [Target]    SELECt * FROM @TargetUDT         END

样例代码:

public static void StartProcess()        { //Create a local data table to hold customer records DataTable dtCustomers = new DataTable("Customers"); DataColumn dcFirstName = new DataColumn("FirstName", typeof(string)); DataColumn dcLastName = new DataColumn("LastName", typeof(string)); DataColumn dcEmail = new DataColumn("Email", typeof(string)); dtCustomers.Columns.Add(dcFirstName); dtCustomers.Columns.Add(dcLastName); dtCustomers.Columns.Add(dcEmail); //Add customer 1 DataRow drCustomer = dtCustomers.NewRow(); drCustomer["FirstName"] = "AAA"; drCustomer["LastName"] = "XYZ"; drCustomer["Email"] = "aaa@test.com"; dtCustomers.Rows.Add(drCustomer); //Add customer 2 drCustomer = dtCustomers.NewRow(); drCustomer["FirstName"] = "BBB"; drCustomer["LastName"] = "XYZ"; drCustomer["Email"] = "bbb@test.com"; dtCustomers.Rows.Add(drCustomer); //Add customer 3 drCustomer = dtCustomers.NewRow(); drCustomer["FirstName"] = "CCC"; drCustomer["LastName"] = "XYZ"; drCustomer["Email"] = "ccc@test.com"; dtCustomers.Rows.Add(drCustomer); //Create Connection object to connect to server/database SqlConnection conn = new SqlConnection(ConStr); conn.Open(); //Create a command object that calls the stored procedure SqlCommand cmdCustomer = new SqlCommand("AddToTarget", conn); cmdCustomer.CommandType = CommandType.StoredProcedure; //Create a parameter using the new SQL DB type viz. Structured to pass as table value parameter SqlParameter paramCustomer = cmdCustomer.Parameters.Add("@TargetUDT", SqlDbType.Structured); paramCustomer.Value = dtCustomers; //Execute the query cmdCustomer.ExecuteNonQuery();     }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376562.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号