【问题标题】:Insert a dataSet into a SQL table将数据集插入 SQL 表
【发布时间】:2013-12-02 15:23:20
【问题描述】:

我有一个数据集,其中包含来自 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();
}

我应该怎么做才能将数据集保存在空表中?

谢谢

【问题讨论】:

    标签: c# sql dataset sqldataadapter


    【解决方案1】:

    可以参考以下代码:

    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 中转换它。

    文档参考:

    http://msdn.microsoft.com/en-us/library/system.data.common.dbdataadapter.insertcommand(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

    【讨论】:

    • 我有一个由 3 个表填充的数据集。我从 3 个不同的表中选择了数据并将它们添加到我的数据集中。现在我想将该数据集保存在 sql 中的一个空表中。我已经创建了表格...谢谢
    • @user2967732 在您的查询中您只提到 1 : select * from tblTailleALL
    • 这是一种方法。我在我的方法中传递了一个数据集。我想用数据集中的数据更新我的空表。但错误是由于表是空的,我无法更新。我会收到并发错误。
    • @user2967732 如果有多个表,您需要在此处创建数据行:stackoverflow.com/questions/1791042/…
    【解决方案2】:

    在您的数据库中创建一个用户定义的 TableType:

    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
    

    并将您的 DataTable 直接发送到 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();
    

    【讨论】:

    • 我必须在数据表中转换我的数据集。所以,我应该这样做。数据表 dt = MyDataSet.Tables[0];好吧,我真的很感激,这确实让我走对了!感谢您的友好和明智的回答。
    • 我还没有实现你的解决方案,一旦我做到了,我会给你一个反馈。
    • 我已经在 SQL 中创建了我的表。我只需要将数据集数据存储到该表中。我的表名是 tblTailleALL。
    • 请提供一些额外的细节我不明白你想要什么
    猜你喜欢
    • 1970-01-01
    • 2017-08-22
    • 2011-06-12
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    相关资源
    最近更新 更多