c#如何对access进行增删改查

小小痴语者 2012-08-08 10:14:24
textbox控件,如何对access里面的表的数据进行增删改查,我是新手,求高手指点啊,都两天了,还是没有想出来,求具体步骤
...全文
429 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
小小痴语者 2012-08-10
  • 打赏
  • 举报
回复
求推荐觉得最经典最好的入门书籍……
hard_learner 2012-08-09
  • 打赏
  • 举报
回复
你就不能塌下身子找本基础的书看下吗?有问的时间早就把原理都搞明白了
小小痴语者 2012-08-09
  • 打赏
  • 举报
回复
就是说现在有一个ACCESS表了,里面有admin,admin有三个字段:ad_account,ad_password,ad_authority;现在打开vs2005,然后拖个窗口,里面拖三个textbox控件,一个按钮,现在想点击按钮,把三个textbox控件的数据添加到access表的admin里,问现在该怎么办,求大神们具体指点啊……
小小痴语者 2012-08-08
  • 打赏
  • 举报
回复
新手不懂啊,求大神具体点啊……
小小痴语者 2012-08-08
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace 客户跟踪系统
{
public partial class add_admin : Form
{
public add_admin()
{
InitializeComponent();
}

public string ad_account
{
get
{ return this.textBox1.Text; }
set
{ this.textBox1.Text = value; }
}

public string ad_password
{
get
{ return this.textBox2.Text; }
set
{ this.textBox2.Text = value; }
}

public string ad_authority
{
get
{ return this.textBox3.Text; }
set
{ this.textBox3.Text = value; }
}

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
{
string sql = "insert into admin values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
int i = ReturnNonQuery(sql, false);
if (i > 0)
{
MessageBox.Show("插入成功");
}
else
{
MessageBox.Show("插入失败");
}

}
else
{ MessageBox.Show("请填写完整的资料!", "错误提示:", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}

public int ReturnNonQuery(string sql, bool isproc, params SqlParameter[] p)
{
string constr = @"server=.;database=客户跟踪系统;";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(sql, con);

//如果是存储过程,执行 否者 执行语句
int j = 0;
if (isproc == true)
{
cmd.CommandType = CommandType.StoredProcedure;
}
for (int i = 0; i < p.Length; i++)
{
cmd.Parameters.Add(p[i]);
}
try
{
con.Open();
j = cmd.ExecuteNonQuery();
return j;
}
catch (Exception ex)
{
throw ex;


}
finally
{
con.Dispose();
}

}





private void admin(string ad_account,string ad_password,string ad_authority)
{
throw new Exception("The method or operation is not implemented.");
}

private void values(char p,char p_2,char p_3)
{
throw new Exception("The method or operation is not implemented.");
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}



}
}


我按你的方式试了一下,没有错误,但是也没有实现添加的效果,数据库的数据没有增加一行,还刚才的一样,是数据库没有设置好么,怎么设置的啊,求大神们指点啊……
kingdom_0 2012-08-08
  • 打赏
  • 举报
回复
建表
连接数据库
对表中的数据具体GUID操作
牛哥_ 2012-08-08
  • 打赏
  • 举报
回复

private void button1_Click(object sender, EventArgs e)
{
string sql = "insert into table values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
int i = ReturnNonQuery(sql,false);
if (i > 0)
{
MessageBox.Show("插入成功");
}
else
{
MessageBox.Show("插入失败");
}
}

public int ReturnNonQuery(string sql, bool isproc, params SqlParameter[] p)
{
string constr = @"server=.;database=你的库;uid=sa;pwd=sa";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(sql, con);

//如果是存储过程,执行 否者 执行语句
int j = 0;
if (isproc == true)
{
cmd.CommandType = CommandType.StoredProcedure;
}
for (int i = 0; i < p.Length; i++)
{
cmd.Parameters.Add(p[i]);
}
try
{
con.Open();
j = cmd.ExecuteNonQuery();
return j;
}
catch (Exception ex)
{
throw ex;


}
finally
{
con.Dispose();
}

}


没测试 自己看着改吧
小小痴语者 2012-08-08
  • 打赏
  • 举报
回复
是数据库连接的问题的么,那个问题是怎么设置的啊,这个也不太懂,求高手随手一起指点了……
小小痴语者 2012-08-08
  • 打赏
  • 举报
回复
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
{
OleDbConnection con8 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "客户跟踪系统.mdb" + ";Persist Security Info=False");
string sql = "insert into admin values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
OleDbDataAdapter dap8 = new OleDbDataAdapter(sql, con8);
DataSet ds8 = new DataSet();
dap8.Fill(ds8, "admin");
}
else
{ MessageBox.Show("请填写完整的资料!", "错误提示:", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
我按你的方式试了一下,没有错误,但是也没有实现添加的效果,数据库的数据没有增加一行,这是什么情况,求指点
  • 打赏
  • 举报
回复
少了}括号
  • 打赏
  • 举报
回复

private void 添加信息按钮_Click(object sender, EventArgs e)

{
OleDbConnection con8 = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "员工信息.mdb" + ";Persist Security Info=False");
string sql = "insert into 雇员 values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')";
OleDbDataAdapter dap8 = new OleDbDataAdapter(sql, con8);
DataSet ds8 = new DataSet();
dap8.Fill(ds8, "雇员");
  • 打赏
  • 举报
回复
先把你的表建立好
小小痴语者 2012-08-08
  • 打赏
  • 举报
回复
这些都太复杂,不知道怎么用,我现在就是在一个窗口中添加了3个textbox控件,一个添加按钮,现在想要点击添加,把3个textbox控件里的值存到ACCESS数据库中的admin表中,表中有三个字段,ad_account,ad_password,ad_authority,这个按钮的代码怎么写啊,求大神指点具体点的代码……

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧