using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using ADOX;
using System.Data.OleDb;
namespace _2_Course_Work
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection DBConn;
OpenFileDialog openfiledialog;
OleDbDataAdapter adapter;
DataSet ds;
OleDbCommand command;
int vtable = 0; //количество таблиц, которые совпали
string NTName = "NewTableName"; //Название новой таблицы
private void button1_Click(object sender, EventArgs e)
{
ADOX.Catalog cat = new ADOX.Catalog();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=D:\\NewMDB.mdb;" +
"Jet OLEDB:Engine Type=5");
cat = null;
}
private void button2_Click(object sender, EventArgs e)
{
openfiledialog = new OpenFileDialog();
openfiledialog.Filter = "DataBase (*.mdb; *.accdb)|*.mdb; *.accdb;";
openfiledialog.Multiselect = true;
comboBox1.Items.Clear();
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
foreach (String file in openfiledialog.FileNames)
{
DBConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file);
DBConn.Open();
DataTable datatable = DBConn.GetSchema("Tables", new string[] { null, null, null, "TABLE" });
foreach (DataRow row in datatable.Rows)
{
string TableName = row["TABLE_NAME"].ToString();
comboBox1.Items.Add(TableName+","+file);
}
// OleDbCommand com = new OleDbCommand("CREATE TABLE Результаты (номер COUNTER CONSTRAINT номер PRIMARY KEY,Тема STRING, Дата string ,Группа STRING, Студент STRING ,Верных_ответов STRING,Результат STRING )", DBConn);
// com.ExecuteNonQuery();
DBConn.Close();
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var param = comboBox1.SelectedItem.ToString().Split(',');
DBConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + param[1]);
DBConn.Open();
adapter = new OleDbDataAdapter(@"SELECT * FROM " + param[0], DBConn);
DataTable datatable1 = new DataTable();
adapter.Fill(datatable1);
ds = new DataSet();
ds.Tables.Add(datatable1);
/*DataRow row = datatable1.NewRow();
row["Город"] = "Artem";
datatable1.Rows.Add(row);*/
dataGridView1.DataSource = datatable1;
// MessageBox.Show(dataGridView1.Columns[2].HeaderText);
DBConn.Close();
}
private void button3_Click(object sender, EventArgs e)
{
//DBConn.Open();
//adapter.Update(ds.Tables[0]);
//UpdateBD(comboBox1.SelectedItem.ToString(), "Город", "УФА");
// DBConn.Close();
OleDbConnection NewDB = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\NewMDB.mdb");
NewDB.Open();
OleDbCommand command = NewDB.CreateCommand();
command.CommandText = "INSERT INTO Города (КодГорода) VALUES (?)";
command.Parameters.Add("", OleDbType.Char).Value = "56";
/*string ct = "INSERT INTO Города1 (КодГорода, Город, КодСтраны) VALUES (32, ГЫТЫА, 123)";
OleDbCommand com = new OleDbCommand(ct, NewDB);*/
command.ExecuteNonQuery();
NewDB.Close();
}
private void UpdateBD(string tablename, string colmn, string value,int r,OleDbConnection c)
{
adapter = new OleDbDataAdapter("select * from " + tablename,c);
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
ds = new DataSet();
adapter.Fill(ds,tablename);
ds.Tables[0].Rows[r][colmn] = value;
adapter.Update(ds,tablename);
}
private void InsertBD(string tablename, string colmn,string type, string value, string direct)
{
OleDbConnection NewDB = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+direct);
NewDB.Open();
OleDbCommand command = NewDB.CreateCommand();
string v ="";
var param = value.Split(',');
for (int t = 0; t < param.Length; t++)
{
v += "?,";
}
command.CommandText = "INSERT INTO "+tablename+" ("+colmn+") VALUES ("+v.TrimEnd(',',' ')+")";
for (int t = 0; t < param.Length; t++)
{
command.Parameters.Add("", OleDbType.Char).Value = param[t];
}
// MessageBox.Show(command.CommandText);
/*if (type == "Integer") command.Parameters.Add("", OleDbType.Integer).Value = value;
else */
//command.Parameters.Add("", OleDbType.Char).Value = value;
command.ExecuteNonQuery();
NewDB.Close();
}
private void button4_Click(object sender, EventArgs e)
{
foreach (String file in openfiledialog.FileNames)
{
DBConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file);
DBConn.Open();
DataTable datatable = DBConn.GetSchema("Tables", new string[] { null, null, null, "TABLE" });
foreach (DataRow row in datatable.Rows)
{
string TableName = row["TABLE_NAME"].ToString();
if (vtable == 0) NTName = TableName;
adapter = new OleDbDataAdapter(@"SELECT * FROM " + TableName, DBConn);
DataTable datatable1 = new DataTable();
adapter.Fill(datatable1);
ds = new DataSet();
ds.Tables.Add(datatable1);
dataGridView1.DataSource = datatable1;
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (dataGridView1.Columns.HeaderText == SClmn.Text)
{
string type;
OleDbConnection NewDB = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\NewMDB.mdb");
NewDB.Open();
if (vtable == 0)
{