如何使用C#在Access中创建、更新、删除数据?
我正在使用 Microsoft Visual C# 2008 Express Edition 创建此项目。 我想使用单选按钮插入数据如何编写代码,例如,我想插入性别(男/女)。请帮我编写代码,
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection con;
DataSet ds1;
OleDbDataAdapter da;
int MaxRows = 0;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
con = new.OleDbConnection();
ds1 = new DataSet();
con.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/MyWorkers1.mdb";
string sql = "SELECT * from tblWorkers";
da = new OleDbDataAdapter(sql, con);
con.Open();
da.Fill(ds1, "MyWorkers1");
NavigateRecords();
MaxRows = ds1.Tables["MyWorkers1"].Rows.Count;
//MaxRows = ds1.Tables["MyWorkers1"].Rows[inc];
//MessageBox.Show("Database open");
con.Close();
//MessageBox.Show("Database close");
con.Dispose();
}
private void NavigateRecords()
{
DataRow drow = ds1.Tables["MyWorkers1"].Rows[inc];
textBox1.Text = drow.ItemArray.GetValue(0).ToString();
textBox2.Text = drow.ItemArray.GetValue(1).ToString();
textBox3.Text = drow.ItemArray.GetValue(2).ToString();
}
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc++;
NavigateRecords();
}
else
{
MessageBox.Show("No More Records");
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{
inc--;
NavigateRecords();
}
else
{
MessageBox.Show("First Record");
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
if (inc != 0)
{
inc = 0;
NavigateRecords();
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc = MaxRows - 1;
NavigateRecords();
}
}
private void btnAddNew_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void btnSave_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbCommandBuilder cb;
cb = new System.Data.OleDb.OleDbCommandBuilder(da);
DataRow drow = ds1.Tables["MyWorkers1"].NewRow();
drow[0] = textBox1.Text;
drow[1] = textBox2.Text;
drow[2] = textBox3.Text;
ds1.Tables["MyWorkers1"].Rows.Add(drow);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
da.Update(ds1, "MyWorkers1");
MessageBox.Show("Record / Entry Added");
}
}
}
运行此代码时,它会在 da.Update(ds1, "MyWorkers1");` 中显示错误
无效的OperationException未处理的连接属性尚未初始化
请帮助我。
I am creating this project using Microsoft Visual C# 2008 Express Edition.
I want insert data using radio buttons how to write the code for example, i want insert gender (male/female). Please help me to write the code
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection con;
DataSet ds1;
OleDbDataAdapter da;
int MaxRows = 0;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
con = new.OleDbConnection();
ds1 = new DataSet();
con.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/MyWorkers1.mdb";
string sql = "SELECT * from tblWorkers";
da = new OleDbDataAdapter(sql, con);
con.Open();
da.Fill(ds1, "MyWorkers1");
NavigateRecords();
MaxRows = ds1.Tables["MyWorkers1"].Rows.Count;
//MaxRows = ds1.Tables["MyWorkers1"].Rows[inc];
//MessageBox.Show("Database open");
con.Close();
//MessageBox.Show("Database close");
con.Dispose();
}
private void NavigateRecords()
{
DataRow drow = ds1.Tables["MyWorkers1"].Rows[inc];
textBox1.Text = drow.ItemArray.GetValue(0).ToString();
textBox2.Text = drow.ItemArray.GetValue(1).ToString();
textBox3.Text = drow.ItemArray.GetValue(2).ToString();
}
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc++;
NavigateRecords();
}
else
{
MessageBox.Show("No More Records");
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{
inc--;
NavigateRecords();
}
else
{
MessageBox.Show("First Record");
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
if (inc != 0)
{
inc = 0;
NavigateRecords();
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc = MaxRows - 1;
NavigateRecords();
}
}
private void btnAddNew_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void btnSave_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbCommandBuilder cb;
cb = new System.Data.OleDb.OleDbCommandBuilder(da);
DataRow drow = ds1.Tables["MyWorkers1"].NewRow();
drow[0] = textBox1.Text;
drow[1] = textBox2.Text;
drow[2] = textBox3.Text;
ds1.Tables["MyWorkers1"].Rows.Add(drow);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
da.Update(ds1, "MyWorkers1");
MessageBox.Show("Record / Entry Added");
}
}
}
When run this it shows error in da.Update(ds1, "MyWorkers1");` like
Invalid OperationException Unhandled connection property has not been intialized
Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 Save 方法中再次打开连接,就像在 FormLoad 方法中一样。
You need to open your connection again in the Save method like you do in the FormLoad method.
您忘记在更新之前打开连接。
最佳实践是在事务之前和之后打开和关闭连接。
首先,不要通过从函数中删除
con.Dispose();
来释放Form1_Load()
中的连接。然后在 btnSave_Click 事件处理程序中的更新调用周围添加以下内容:
这应该可以解决问题。
You forgot to open your connection before the update.
Best practice is to open and close a connection right before and after a transaction.
First off do not dispose the connection in your
Form1_Load()
by removingcon.Dispose();
from the function.Then add the following around your update call in your btnSave_Click eventhandler:
That should do the trick.