C# 未调用函数内部的连接
这是我的代码片段,请帮助我为什么con.open在函数tabledel中不起作用C# 未调用函数内部的连接,c#,forms,ado.net,database-connection,connection-string,C#,Forms,Ado.net,Database Connection,Connection String,这是我的代码片段,请帮助我为什么con.open在函数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;
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();
}
}
}
我想在按下按钮后删除表,第一次打开连接,但在函数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();
}
}
}
正如我看到的,您会遇到以下错误:
The ConnectionString property has not been initialized.
可能您将在除form class全局变量之外的其他地方创建dispose连接,因此我建议使用recreating connection显式设置连接字符串:
con = new OleDbConnection(
@"PROVIDER=Microsoft.ACE.OLEDB.12.0; Data Source=C:/Users/Dinesh/Documents/Database3.accdb");
con.Open();
无论如何,在SO中包含代码的主要部分并不难。连接对象是在创建表单时创建的。您可以在Form_Load方法中打开、使用、关闭和处置连接对象。因此,当按钮单击处理程序开始执行时,连接对象就消失了
您必须在按钮单击处理程序中创建一个新的连接对象,或者不在Form_Load方法中处理它。很难理解您的实际问题是什么,是什么导致了它等等,但是您的代码有很多问题 首先,你不应该像你那样初始化con对象。每次使用完并调用Dispose后,您都会将其销毁。在任何情况下,直到下一次初始化Form1类时,才会重建它。这可能就是为什么你会出错 一个建议是让一个单身汉来处理你的连接。这就引出了下一点:在循环中打开新连接是一个非常糟糕的主意。如果你已经正确地建立了连接,那么每次通话只需打开一个连接。我将为您提供更好的性能,如果有许多元素需要迭代,您会注意到这一点 接下来,delete语句将接受SQL注入。您应该强烈考虑使用一种最小化用户输入的方式从列表中进行选择,这样他们就不会在textBox1.Text中键入奇怪的查询 这只是其中的一部分。。我希望你能使用它
哦,请,请写一个更好的问题。不要只是说,有些事情不管用。链接浓缩您的代码并思考您的问题,以确保其被广泛理解。请在问题本身中提供示例。您尝试过什么?是否已将事件处理程序分配给按钮单击事件?加载表单时将处理连接。除此之外,您的代码还有很多问题。请将代码粘贴到此处。您在con.open上是否有任何错误??请也提一下。下次在这里添加您的代码示例!是的,那么你想让我重新定义函数中的con吗?/@user1386579,是的,我想在某个地方这个属性会设置为null或空。代码太大了,我没有仔细阅读。谢谢,我道歉,下次我会发布更好的问题,我有点匆忙