【问题标题】:How to pass Datatable to SQL Server using asp.net如何使用 asp.net 将数据表传递给 SQL Server
【发布时间】:2014-04-22 10:15:36
【问题描述】:

我正在开发一个 asp.net Web 应用程序,我正在将一个 DataTable 从一个 asp.net 应用程序传递到 SQL Server 存储过程。

我在 SQL Server 中的表是

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

在此表中,ID 是主键并自动递增。我的 C# 代码在按钮单击时将 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);
    }
}

我在 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

我创建了一个表类型为:

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

但是当我单击按钮将DataTable 插入Student 表时,我收到此错误:

将数据类型 nvarchar 转换为 bigint 时出错。
表值参数“@Student”的数据不符合参数的表类型。

请帮帮我。

【问题讨论】:

    标签: c# asp.net sql-server datatable


    【解决方案1】:

    我唯一能想到的是它不是按名称映射列,而是按索引映射,所以尝试反转列定义,看看会发生什么:

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

    希望它有效。

    【讨论】:

      【解决方案2】:

      尝试在 SQL 中使用打印命令打印@Student 参数值,一旦获得动态生成的完整 SQL 字符串,尝试执行在 Student_Insert_Update_Delete 存储过程中编写的完整 sql 语句,您将了解您的陈述中缺少什么

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多