[英]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.