C# Inserting a DataSet Into SQL Database

☆樱花仙子☆ 提交于 2019-12-12 01:36:42

问题


I'm trying to insert a dataset into an SQL database but I am having difficulties passing my dataset as an argument to my DB class. I am not sure if it is allowed to pass as an argument. If not, what are my alternatives?

The way I create my dataset:

public static void getLogs() {
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\someDir";
    SQLiteConnection cn = new SQLiteConnection("Data Source=" + path + ";Version=3;New=False;Compress=True;");
    cn.Open();
    SQLiteDataAdapter sd = new SQLiteDataAdapter("SELECT * FROM table", cn);

    DataSet ds = new DataSet();
    sd.Fill(ds);

    cn.Close();

    db.InsertLogs(Form1.adminID, Form1.deviceID, ds);
}

My database class and insert method looks like the following:

public void InsertLogs(string user_id, string device_id, DataSet history)
{
    string query = "INSERT INTO table (column1, column2, column3, column4, column5, column6, column7) VALUES (@value1, @value2, @value3, @value4, @value5, @value6, @value7);";

    if (OpenConnection() == true)
    {
        foreach (DataTable table in history.Tables)
        {
            foreach (DataRow row in table.Rows)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.Parameters.AddWithValue("@value1", int.Parse(user_id));
                cmd.Parameters.AddWithValue("@value2", int.Parse(device_id));
                cmd.Parameters.AddWithValue("@value3", row[0]);
                cmd.Parameters.AddWithValue("@value4", row[1]);
                cmd.Parameters.AddWithValue("@value5", row[2]);
                cmd.Parameters.AddWithValue("@value6", row[3]);
                cmd.Parameters.AddWithValue("@value7", row[4]);
                cmd.ExecuteNonQuery();
            }
        }
        CloseConnection();
    }
}

Thank you


回答1:


you can loop through datatables in a dataset and can pass a datatable as a stored procedure paramater, found an example here




回答2:


1.- Go to SQL Server, under your DB name go to "programmability\Types\User-Defined Table Types, right click and create a new one:

USE DBNAME
GO

-- Create the data type
CREATE TYPE ValuesToInsert AS TABLE 
(
    Value1 INT NOT NULL,
    Value2 INT NOT NULL,
    Value3 VARCHAR(20)
)
GO

2.- Create a SP to receive the table as parameter, parameter must be the new User-Defined table type created in step 1

USE [DBNAME]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- 
CREATE PROCEDURE [dbo].[spImportData]
    @DataImported dbo.ValuesToInsert READONLY
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO [dbo].[TableName] (Value1, Value2, Value3)
    SELECT Value1, Value2, Value3
    FROM @DataImported 

3.- Pass a datatable from your code to DB, in this case using Dapper.net as following:

DataTable dtExcelData = new DataTable();
 //Fill dtExcelData and pass as parameter
 ParametersCollection param = new ParametersCollection();
 param.Add(CreateParameter("@DataImported", dtExcelData));
 ExecuteDataSet("spImportData", CommandType.StoredProcedure, param);


来源:https://stackoverflow.com/questions/43083962/c-sharp-inserting-a-dataset-into-sql-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!