这是我的片段,请帮助我为什么 con.open 在函数 tabledel 中不起作用
一旦我按下按钮,我想删除表,第一次打开连接,但在函数 tabledel 内第二次无法打开它
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WFA_CREATE_DELETE
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection con = new OleDbConnection(@"PROVIDER=Microsoft.ACE.OLEDB.12.0; Data Source=C:/Users/Dinesh/Documents/Database3.accdb");
OleDbDataAdapter ea = new OleDbDataAdapter();
DataSet dsl;
DataSet esl;
private void Form1_Load(object sender, EventArgs e)
{
OleDbDataAdapter da = new OleDbDataAdapter();
dsl = new DataSet();
con.Open();
DataTable table2 = con.GetSchema("tables");
MessageBox.Show("Database Open");
dataGridView1.DataSource = table2;
con.Close();
con.Dispose();
}
public void Tabledel()
{
int a = 0, d = 0, count, itr;
count = dataGridView1.RowCount;
itr = dataGridView1.ColumnCount;
while (a < count)
{
for (d = 0; d < itr; d++)
{
if (dataGridView1.Rows[a].Cells[d].Value.ToString() == textBox1.Text)
{
MessageBox.Show("table exists");
esl = new DataSet();
string vsql = "drop table '" + textBox1.Text + "'";
ea = new System.Data.OleDb.OleDbDataAdapter(vsql, con);
OleDbCommand cmdea = new OleDbCommand(vsql, con);
//cmdea.Connection = con;
con.Open();
cmdea.ExecuteNonQuery();
MessageBox.Show("table dropped");
con.Close();
con.Dispose();
}
}
a++;
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
public void button1_Click(object sender, EventArgs e)
{
Tabledel();
}
}
}