1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
| 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.Data.OleDb;
namespace OleDB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: данная строка кода позволяет загрузить данные в таблицу "testDataSet.Cartrige". При необходимости она может быть перемещена или удалена.
this.cartrigeTableAdapter.Fill(this.testDataSet.Cartrige);
string mytablename = "tableTest";
//string mybdpath = "G:\\test.mdb";
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "G:\\test.mdb";
OleDbConnection connection = new OleDbConnection(conStr);
OleDbDataAdapter adapter = new OleDbDataAdapter();
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * FROM " + mytablename, connection);
connection.Close();
adapter.SelectCommand = command;
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
adapter.Update(dataSet);
}
private void button1_Click(object sender, EventArgs e)
{
//comboBox1.Items.Clear();
//OleDbConnection dbCon = new OleDbConnection(
// @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\\test.mdb");
//dbCon.Open();
//DataTable tbls = dbCon.GetSchema("tables", new string[] { null, null, null, "TABLE" }); //список всех таблиц
//foreach (DataRow row in tbls.Rows)
//{
// string TableName = row["TABLE_NAME"].ToString();
// comboBox1.Items.Add(TableName);
//}
//dbCon.Close();
//OleDbCommand cmd = new OleDbCommand();
//cmd.CommandText = "NSERT INTO tableTest VALUES (1,2,3)";
//int rowsAffected = cmd.ExecuteNonQuery();
//System.Windows.Forms.MessageBox.Show(rowsAffected.ToString());
//string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"G:\\test.mdb";
//textBox4.Text = dateTimePicker1.Value.ToString("dd.MM.yyyy");
OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\\test.mdb");
OleDbDataAdapter thisAdapter = new OleDbDataAdapter();
thisAdapter.InsertCommand = new OleDbCommand("INSERT INTO tableTest ([name], [count], [sum], [date]) values (?, ?, ?, ?)", thisConnection);
thisAdapter.InsertCommand.Parameters.Add("@name", OleDbType.VarChar).Value = comboBox1.Text;
thisAdapter.InsertCommand.Parameters.Add("@count", OleDbType.VarChar).Value = textBox2.Text;
thisAdapter.InsertCommand.Parameters.Add("@sum", OleDbType.VarChar).Value = textBox3.Text;
thisAdapter.InsertCommand.Parameters.Add("@date", OleDbType.VarChar).Value = dateTimePicker1.Value.ToString("dd.MM.yyyy");
thisAdapter.SelectCommand = new OleDbCommand("SELECT name,count,sum,date FROM tableTest", thisConnection);
OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);
thisConnection.Open();
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet,"tableTest");
DataRow thisRow = thisDataSet.Tables["tableTest"].NewRow();
//thisRow["Код"] = "0";
thisRow["name"] = comboBox1.Text;
thisRow["count"] = textBox2.Text;
thisRow["sum"] = textBox3.Text;
thisRow["date"] = dateTimePicker1.Value.ToString("dd.MM.yyyy");
thisDataSet.Tables["tableTest"].Rows.Add(thisRow);
// thisAdapter.ContinueUpdateOnError = true;
thisAdapter.Update(thisDataSet, "tableTest");
thisConnection.Close();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
string mytablename = "tableTest";
//string mybdpath = "G:\\test.mdb";
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "G:\\test.mdb";
OleDbConnection connection = new OleDbConnection(conStr);
OleDbDataAdapter adapter = new OleDbDataAdapter();
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * FROM " + mytablename, connection);
connection.Close();
adapter.SelectCommand = command;
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
adapter.Update(dataSet);
}
private void button2_Click(object sender, EventArgs e)
{
//OleDbConnection dbCon = new OleDbConnection(
// @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\\test.mdb");
//dbCon.Open();
//OleDbDataAdapter dbAdapter1 = new OleDbDataAdapter(@"SELECT " + comboBox1.SelectedItem + @".* FROM " + comboBox1.SelectedItem, dbCon);
//dbAdapter1.InsertCommand = new OleDbCommand("INSERT INTO tableTest ([name], [count], [sum]) values (?, ?, ?)", dbCon);
//DataTable dataTable = new DataTable();
//dbAdapter1.Fill(dataTable + ".*");
//dataGridView1.DataSource = dataTable;
//dbCon.Close();
//try
//{
// string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\\test.mdb";
// OleDbConnection oleCon = new OleDbConnection(conStr);
// oleCon.Open();
// OleDbCommand nn = oleCon.CreateCommand();
// nn.CommandText = "SELECT * FROM tableTest";
// nn.ExecuteNonQuery();
// oleCon.Close();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
//string mytablename = "tableTest";
//string mybdpath = "G:\\test.mdb";
//string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "G:\\test.mdb";
//OleDbConnection connection = new OleDbConnection(conStr);
//OleDbDataAdapter adapter = new OleDbDataAdapter();
//connection.Open();
//OleDbCommand command = new OleDbCommand("SELECT * FROM " + mytablename, connection);
//connection.Close();
//adapter.SelectCommand = command;
//DataSet dataSet = new DataSet();
//adapter.Fill(dataSet);
//dataGridView1.DataSource = dataSet.Tables[0];
//adapter.Update(dataSet);
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
//int n = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[1].Value);
//numericUpDown1.Value = n;
textBox2.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); //удаление
}
else
{
MessageBox.Show("Выберите строку для удаления.", "Ошибка.");
}
}
}
} |