简体   繁体   English

如何使用asp.net将Datatable传递给SQL Server

[英]How to pass Datatable to SQL Server using asp.net

I am working on an asp.net web application where I am passing a DataTable from an asp.net application to SQL Server stored procedure. 我正在开发一个asp.net Web应用程序,我将一个DataTable从一个asp.net应用程序传递给SQL Server存储过程。

My table in SQL Server is 我在SQL Server中的表是

Student(ID bigint, Name nvarchar(max), Reg bigint).

In this table, the ID is the primary key and auto incremented. 在此表中, ID是主键并自动递增。 My c# code to pass a DataTable to stored procedure on button click is: 我点击按钮点击将DataTable传递给存储过程的代码是:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        DataTable dt = new DataTable("Student");

        dt.Columns.Add("Reg", typeof(long));
        dt.Columns.Add("Name", typeof(string));

        // Create a new row
        for (int i = 0; i < 3; i++)
        {
            DataRow NewRow = dt.NewRow();

            if (i == 0)
            {
                NewRow["Name"] = "Raunak";
                NewRow["Reg"] = Convert.ToInt64(1); ;
            }
            if (i == 1)
            {
                NewRow["Name"] = "Vikash";
                NewRow["Reg"] = Convert.ToInt64(1);
            }
            if (i == 2)
            {
                NewRow["Name"] = "Deepak";
                NewRow["Reg"] = Convert.ToInt64(1);
            }

            dt.Rows.Add(NewRow);
        }

        dt.AcceptChanges();

        string constring = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection cnn = new SqlConnection(@"Data Source=.\sqlexpress; Initial Catalog=builderERP; Integrated Security=True;");           

        SqlCommand selectCommand = new SqlCommand("Student_Insert_Update_Delete", cnn);
        selectCommand.CommandType = CommandType.StoredProcedure;
        SqlParameter tvpParam = selectCommand.Parameters.AddWithValue("@Student", dt);
        tvpParam.SqlDbType = SqlDbType.Structured;

        cnn.Open();

        int xx = selectCommand.ExecuteNonQuery();

        if (xx > 0)
        {
            lblMsg.Text = "Data Successfully Inserted.";
        }
        else
        {
            lblMsg.Text = "Data Insertion Failed.";
        }
        cnn.Close();

    }
    catch (Exception ex)
    {
        throw (ex);
    }
}

My stored procedure in T-SQL is : 我在T-SQL中的存储过程是:

CREATE PROCEDURE [dbo].[Student_Insert_Update_Delete]
(
    @Student AS [dbo].[StudentTableVal] Readonly
)
AS
BEGIN   
INSERT INTO Student(Name,Reg) 
        SELECT Name,Reg FROM @Student
        --commit transaction

 END

I have created a table type as: 我创建了一个表类型:

CREATE TYPE StudentTableVal AS TABLE
(
    Name NVARCHAR(max),
    Reg bigint
) 

But when I click the button to insert DataTable into Student table I get this error: 但是,当我单击按钮将DataTable插入Student表时,我收到此错误:

Error converting data type nvarchar to bigint. 将数据类型nvarchar转换为bigint时出错。
The data for table-valued parameter "@Student" doesn't conform to the table type of the parameter. 表值参数“@Student”的数据不符合参数的表类型。

Please help me someone. 请帮帮我一个人。

The only thing I can think of is that it's not mapping the column by name but by index so try to invert the column definition and see what happens: 我唯一能想到的是,它不是按名称而是通过索引映射列,因此请尝试反转列定义并查看会发生什么:

    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Reg", typeof(long));

Hope it works. 希望它有效。

尝试使用Print命令打印SQL中的@Student参数值一旦获得动态生成的完整SQL字符串,请尝试执行Student_Insert_Update_Delete存储过程中编写的完整sql语句,这样您就会知道语句中缺少什么

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM