データセットレコード(C#)をMS Accessdatabaseテーブルに挿入する必要があります。私はレコードの一括挿入を行う必要があります。データセットレコードをデータベースに挿入する
は、どのように私はC#でこれを行うことができ、このリンクで
データセットレコード(C#)をMS Accessdatabaseテーブルに挿入する必要があります。私はレコードの一括挿入を行う必要があります。データセットレコードをデータベースに挿入する
は、どのように私はC#でこれを行うことができ、このリンクで
チェックは、それはあなたにとって有用であるかもしれません。 sqlでデータを取得した後、vb.netコードをc# に変換して、msaccessにインポートしてエクスポートすることができます。タスクのこの種のために
、data adapter抽象化を使用することを検討してください。適切な実装とOleDbCommand
、OleDbDataAdapter
とOleDbConnection
を置き換えることにより、同様にあなたは、データベースエンジンの他のブランドに対して同じことを行うことができます
// Prerequisite: The data to be inserted is available in a DataTable/DataSet.
var data = new DataTable();
data.Columns.Add("CompanyName", typeof(string));
data.Columns.Add("Phone", typeof(string));
data.Rows.Add("Foo", "12345678");
data.Rows.Add("Bar", "87654321");
// Now, open a database connection using the Microsoft.Jet.OLEDB provider.
// The "using" statement ensures that the connection is closed no matter what.
using (var connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=Northwind.mdb"))
{
connection.Open();
// Create an OleDbDataAdapter and provide it with an INSERT command.
var adapter = new OleDbDataAdapter();
adapter.InsertCommand = new OleDbCommand("INSERT INTO Shippers (CompanyName, Phone) VALUES (@CompanyName , @Phone)", connection);
adapter.InsertCommand.Parameters.Add("CompanyName", OleDbType.VarChar, 40, "CompanyName");
adapter.InsertCommand.Parameters.Add("Phone", OleDbType.VarChar, 24, "Phone");
// Hit the big red button!
adapter.Update(data);
}
:以下の例に示すように、Microsoft Accessデータベースを使用すると、OleDbDataAdapter
実装を使用することができますあなたのデータベースエンジンのために。 Microsoft SQL Serverの場合は、接頭辞がSql
のクラスを探します。 SqlCommand
。
これはとても役に立ちました。ありがとうございました – user983924
public void insert_dataset(DataSet ds,string ret_table, string table, string fileds, ArrayList arr_data)
{
ArrayList arr_rec=new ArrayList();
string[] str_fields = fileds.Split(',');
for (int i=0;i<ds.Tables[ret_table].Rows.Count;i++)
{
for (int j = 0; j < str_fields.Length; j++)
{
arr_rec.Add(ds.Tables[ret_table].Rows[i].ItemArray[j]);
}
insert_table(table, fileds, arr_rec);
arr_rec.Clear();
}
}
public void insert_table(string table,string fileds,ArrayList arr_data)
{
string str_command, str_params;
string[] str_fields = fileds.Split(',');
for (int i = 0; i < str_fields.Length; i++)
{
str_fields[i] = "@" + str_fields[i].Trim();
}
str_params = string.Join(",", str_fields);
str_command = "INSERT INTO " + table + "(" + fileds + ") values(" + str_params + ")";
con = new OleDbConnection();
//for sql
//con=new SqlConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + frm_main.cur_directory + "\\db_temp1.mdb;Persist Security Info=True";
//for sql
//con.ConnectionString="server=(local);trusted_connection=yes;database=telephon;";
cmd = con.CreateCommand();
//for sql
//cmd=new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText =str_command;
// cmd.Parameters.AddWithValue("@ACagname", "2");
for (int i = 0; i < arr_data.Count; i++)
{
cmd.Parameters.AddWithValue(str_fields[i],arr_data[i]);
}
cmd.ExecuteNonQuery();
con.Close();
}
コードに情報を追加すると便利です。 –
SQL Serverを仲介者として使用することを推奨しますが、ジョブを直接実行する必要があるのはなぜですか? –
私はこれのような解決策を知っているので、私は彼にこのようにするように言う 私は彼がそれが唯一の提案であると有用かもしれないと言った。 – KuldipMCA