简体   繁体   English

如何从存储过程插入表(用户定义类型)并传递给C#

[英]How insert into table(User-defined type) from Stored procedure and passing to c#

How insert into table(User-defined type) from Stored procedure and passing to c# 如何从存储过程 插入表(用户定义类型)并传递给C#

Table structure: 表结构:

CREATE TABLE [dbo].[RegisterChoiceUserInitial](
    [RegisterChoiceUserInitialID] [int] IDENTITY(1,1) NOT NULL,
    [UserId] [uniqueidentifier] NOT NULL,
    [RegisterChoiceUserInitialJob] [nvarchar](50) NULL,
    [RegisterChoiceUserInitialOrganization] [nvarchar](50) NULL,
    [RegisterChoiceUserInitialUnit] [nvarchar](50) NULL,
    [RegisterChoiceUserInitialMembershipType] [nvarchar](50) NULL,
    [RegisterChoiceUserInitialStart] [nvarchar](10) NULL,
    [RegisterChoiceUserInitialEnd] [nvarchar](10) NULL,
 CONSTRAINT [PK_RegisterChoiceUserInitial] PRIMARY KEY CLUSTERED 
(
    [RegisterChoiceUserInitialID] ASC
)

User-defined type: 用户定义类型:

CREATE TYPE [dbo].[TableTypeInitial] AS TABLE(
    [ID] [int] NULL,
    [InitialJob] [nvarchar](50) NULL,
    [InitialOrganization] [nvarchar](50) NULL,
    [InitialUnit] [nvarchar](50) NULL,
    [InitialMembershipType] [nvarchar](50) NULL,
    [InitialMembershipStart] [nvarchar](10) NULL,
    [InitialMembershipEnd] [nvarchar](10) NULL
)

You should make serialize/deserialize for your type, and write string of serialized object to SQL, then when you need to get it, you read string from table and deserialized it to your type. 您应该为您的类型进行序列化/反序列化,然后将序列化对象的字符串写入SQL,然后在需要获取它时,从表中读取字符串并将其反序列化为您的类型。

EDIT 编辑

about serialize you can read here csharp-tutorial-serialize-objects-to-a-file They use serialize to file, but convert it to string will not be hard 关于序列化,您可以在这里阅读csharp-tutorial-serialize-objects-to-file他们使用序列化到文件,但是将其转换为字符串并不困难

There is no way to return a datatable as output param. 无法将数据表作为输出参数返回。 The only way to return table result from xp to c# is either using xml param or in following way. 将表结果从xp返回到c#的唯一方法是使用xml参数或采用以下方式。

sql scripts : sql脚本:

create procedure sp_returnTable
    body of procedure
    **select * from RegisterChoiceUserInitial**
end

sample c# code: 示例C#代码:

   string connString = "<your connection string>";
    string sql = "name of your sp";

SqlConnection conn = new SqlConnection(connString);

try {
   SqlDataAdapter da = new SqlDataAdapter();
   da.SelectCommand = new SqlCommand(sql, conn);
   da.SelectCommand.CommandType = CommandType.StoredProcedure;

   DataSet ds = new DataSet();   
   da.Fill(ds, "result_name");

   DataTable dt = ds.Tables["result_name"];

   foreach (DataRow row in dt.Rows) {
               //manipulate your data
   }

} catch(Exception e) {
   Console.WriteLine("Error: " + e);
} finally {
   conn.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:将用户定义的类型传递给Oracle存储过程 - C#: Pass a user-defined type to a Oracle stored procedure 从C#将用户定义类型的值作为输入参数传递给Oracle中的存储过程 - Pass value of user-defined type as input parameter to stored procedure in Oracle from C# 将字符串作为表类型从C#代码传递到存储过程 - passing string as a table type from c# code in to stored procedure EF:将表值参数从 C# 传递给用户定义的函数 - EF: Passing a table valued parameter to a user-defined function from C# C#auto生成SQL“用户自定义表类型” - C# auto generate SQL “user-defined table type” SQL Server 2008是否在CLR存储过程中接收用户定义的表类型? - SQL Server 2008 Receive an user-defined table type in a CLR Stored Procedure? 在c#中创建用户定义的表类型以在sql server存储过程中使用 - Create a user defined table type in c# to use in sql server stored procedure C#中具有用户定义类型的Oracle存储过程 - Oracle Stored Procedure with User Defined Type in C# EF Core 执行具有多种用户定义表类型的过程 - EF Core execute procedure with many User-Defined Table Type 传递IEnumerable <string> SQL Server存储过程作为用户定义的表类型参数 - Passing IEnumerable<string> to a SQL Server stored procedure as a user defined table type parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM