结构化类型必须至少有一个字段c#

时间:2021-11-13 02:07:35

What I actually want to do is I want to send a string array to SQL Server. I am using SQL Server 2008.

我想要做的是向SQL Server发送一个字符串数组。我正在使用SQL Server 2008。

This can be marked as duplicate but I am facing a whole other problem when implementing a solution from stactoverflow

这可以被标记为重复,但在实现从stactoverflow实现一个解决方案时,我还面临着另一个问题。

LINK: How to pass an array into a SQL Server stored procedure

链接:如何将数组传递到SQL服务器存储过程中

Here is my C# and stored procedure code

这是我的c#和存储过程代码

C# code:

c#代码:

string[] str = new string[] {"s" , "a" , "k"};

DataTable dt = new DataTable();
dt.Columns.Add("names");

foreach (string item in str)
{
    dt.Rows.Add(item);
}

foreach (DataRow r in dt.Rows)
{
    Console.WriteLine(r["names"].ToString());
}

DataTable tvp = new DataTable();

SqlConnection conn = new SqlConnection("Data Source=SHZAK;Initial Catalog=synchroniztionTesing;Integrated Security=True");
conn.Open();

using (conn)
{
    SqlCommand cmd = new SqlCommand("strpdPassAnStringArray", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    SqlParameter tvparam = cmd.Parameters.AddWithValue("@List", tvp);
    tvparam.SqlDbType = SqlDbType.Structured;

    cmd.ExecuteScalar();
}

Before making stored procedure I created a custom type:

在进行存储过程之前,我创建了一个自定义类型:

CREATE TYPE dbo.arrayOfNames
AS TABLE
(
  name  nvarchar(max)
);

Stored procedure code:

存储过程代码:

CREATE PROCEDURE [dbo].[strpdPassAnStringArray]
      @List AS dbo.arrayOfNames READONLY
AS
BEGIN
    SET NOCOUNT ON;

    SELECT name FROM @List; 
END

But in application code it is raising this exception:

但是在应用程序代码中,它引发了一个例外:

There are not enough fields in the Structured type. Structured types must have at least one field.

在结构化类型中没有足够的字段。结构化类型必须至少有一个字段。

on line

在网上

cmd.ExecuteScalar();

1 个解决方案

#1


3  

ANSWER:

答:

I was so dumb to ask that Question , but at least now i know that what causes this exception

我很愚蠢地问了这个问题,但至少现在我知道是什么导致了这个例外

thanks to @Gusman

If your DataTable is empty as it was in my case this exception can be raised

如果您的DataTable是空的,就像在我的例子中一样,则可以引发此异常

        string[] str = new string[] {"sheraz" , "ahmed" , "khan"};
        DataTable tvp = new DataTable();


        tvp.Columns.Add("names");
        foreach (string item in str)
        {
            tvp.Rows.Add(item);
        }

        foreach (DataRow r in tvp.Rows)
        {
            Console.WriteLine(r["names"].ToString());
        }

        SqlConnection conn = new SqlConnection("Data Source=SHZAK;Initial Catalog=synchroniztionTesing;Integrated Security=True");
        conn.Open();

        using (conn)
        {
            SqlCommand cmd = new SqlCommand("strpdPassAnStringArray", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter tvparam = cmd.Parameters.AddWithValue("@List", tvp);
            tvparam.SqlDbType = SqlDbType.Structured;
            cmd.ExecuteScalar();
        }

#1


3  

ANSWER:

答:

I was so dumb to ask that Question , but at least now i know that what causes this exception

我很愚蠢地问了这个问题,但至少现在我知道是什么导致了这个例外

thanks to @Gusman

If your DataTable is empty as it was in my case this exception can be raised

如果您的DataTable是空的,就像在我的例子中一样,则可以引发此异常

        string[] str = new string[] {"sheraz" , "ahmed" , "khan"};
        DataTable tvp = new DataTable();


        tvp.Columns.Add("names");
        foreach (string item in str)
        {
            tvp.Rows.Add(item);
        }

        foreach (DataRow r in tvp.Rows)
        {
            Console.WriteLine(r["names"].ToString());
        }

        SqlConnection conn = new SqlConnection("Data Source=SHZAK;Initial Catalog=synchroniztionTesing;Integrated Security=True");
        conn.Open();

        using (conn)
        {
            SqlCommand cmd = new SqlCommand("strpdPassAnStringArray", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter tvparam = cmd.Parameters.AddWithValue("@List", tvp);
            tvparam.SqlDbType = SqlDbType.Structured;
            cmd.ExecuteScalar();
        }