C# 将数据集插入SQL表中

C# 将数据集插入SQL表中,c#,sql,dataset,sqldataadapter,C#,Sql,Dataset,Sqldataadapter,我有一个数据集,里面充满了来自3个不同表的数据。我想将该数据集存储在SQL中的空表中,我已经创建了该表 public void SaveDataBaseTailleALL(DataGridView dataGridViewRALSelectedTaille, DataSet oDSALL) { PointageCls.totalPointage(dataGridViewRALSelectedTaille); SqlConnection cn = new SqlConnection

我有一个数据集,里面充满了来自3个不同表的数据。我想将该数据集存储在SQL中的空表中,我已经创建了该表

public void SaveDataBaseTailleALL(DataGridView dataGridViewRALSelectedTaille, DataSet oDSALL)
{
    PointageCls.totalPointage(dataGridViewRALSelectedTaille);
    SqlConnection cn = new SqlConnection();
    cn.ConnectionString = "Data Source=HOOSSEN-HP\\INFO2;Initial Catalog=SuiviOF;User ID=sa;Password= PROTECTED;"
    //string strSQL = ")";

    SqlDataAdapter adapt = new SqlDataAdapter("select * from tblTailleALL)", cn);
    SqlCommandBuilder builder = new SqlCommandBuilder(adapt);
    adapt.update(oDSALL.Tables[0]);
    oDSALL.Tables[0].AcceptChanges();
}
如何将数据集保存在空表中


谢谢

您可以参考以下代码:

public static OleDbDataAdapter CreateCustomerAdapter(
    OleDbConnection connection)
{
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command;

    // Create the SelectCommand.
    command = new OleDbCommand("SELECT CustomerID FROM Customers " +
        "WHERE Country = ? AND City = ?", connection);

    command.Parameters.Add("Country", OleDbType.VarChar, 15);
    command.Parameters.Add("City", OleDbType.VarChar, 15);

    adapter.SelectCommand = command;

    // Create the InsertCommand.
    command = new OleDbCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)", connection);

    command.Parameters.Add(
        "CustomerID", OleDbType.Char, 5, "CustomerID");
    command.Parameters.Add(
        "CompanyName", OleDbType.VarChar, 40, "CompanyName");

    adapter.InsertCommand = command;
    return adapter;
}
它是OLEDB,但在语法方面,您可以在sql中转换它

文件参考:


在数据库中创建用户定义的表类型:

CREATE TYPE [dbo].[TableType] AS TABLE(
[UserId] int NOT NULL,
[UserName] [nvarchar](128) NULL,
[Password] [varchar](30)
)

并在存储过程中定义一个参数:

CREATE PROCEDURE [dbo].[InsertTable]
@myTableType TableType readonly
AS
BEGIN
insert into [dbo].Users select * from @myTableType 
END
并将数据表直接发送到sql server:

 SqlCommand command = new SqlCommand("InsertTable");
 command.CommandType = CommandType.StoredProcedure;
 var dt = new DataTable(); //create your own data table
 command.Parameters.Add(new SqlParameter("@myTableType", dt));
 command.ExecuteNonQuery();

我有一个由3个表填充的数据集。我从3个不同的表中选择了数据,并将它们添加到我的数据集中。现在我想将该数据集保存在sql中的空表中。我已经创建了表。。。Thanks@user2967732在您的查询中,您只提到了1:select*from tbltailleall这是一个方法。我在方法中传递了一个数据集。我想用数据集中的数据更新我的空表。但错误是,由于表是空的,我无法更新。我将得到并发错误。@user2967732如果有多个表,您需要按如下方式创建数据行:我必须在数据表中转换数据集。所以,我应该这样做。DataTable dt=MyDataSet.Tables[0];嗯,我真的很感激,这确实让我走上了正确的道路!谢谢你的善意和分步回答。我还没有实现你的解决方案,一旦我实现了,我会给你一个反馈。我已经用SQL创建了我的表。我只需要将数据集数据存储到该表中。我的表名是TBLTAILLALL。请提供一些额外的细节,我不明白您想要什么