gpt4 book ai didi

c# - 将数据集插入到 SQL 表中

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:18 25 4
gpt4 key购买 nike

我有一个数据集,其中包含来自 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 中转换它。

文档引用:

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

关于c# - 将数据集插入到 SQL 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com