C#winform程序对access数据库进行增删查改操作源码,叮当小马的就不用发了
展开全部
不懂。你这是要OLE得操作方法么?
算了。代码给出。我这个是SQL得。你改成OLEDB的就能用了。够详细吧!
===========================================================
string conn="Server=服务器名;Database=数据库名;uid=数据库用户名;pwd=数据库密码";
SqlConnection sqlconn=new SqlConnection(conn);//连接到数据库
=========================================================
表的基本操作的语句要SQL的:
添加语句:Insert into 表名 values(字段名1,字段名2)
修改语句:Update 表明
set 字段名1=修改的值,字段名1=修改的值
where 字段名=所修改的记录字段名
删除语句:delete 表名 where 字段名=所要删除字段的名称
查询语句:select * from 表名
=================================================================
例:
数据库名为:Test, 数据库用户名为:sa , 数据库密码:123
有表名为:Admin
该表的字段:id(int)、name(varchar(8))、pwd(varchar(8))
id name pwd
1 aa 123
2 bb 456
3 cc 789
=========================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public string sql;
protected void Page_Load(object sender, EventArgs e)
{
sql = "select * from Admin"; //查询语句
bind(sql);
}
protected void bind(string sql)
{
string conn="Server=.;DataBase=Test;uid=sa;pwd=123";
SqlConnection sqlconn=new SqlConnection(conn);
SqlCommand cmd=new SqlCommand(sql,sqlconn);
sqlconn.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource=dr;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
sql = "delete Admin where id=" + Convert.ToInt32(TextBox1.Text);//删除语句
string conn = "Server=.;DataBase=Test;uid=sa;pwd=123";
SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(sql, sqlconn);
sqlconn.Open();
cmd.ExecuteReader();
bind("select * from Admin");
}
protected void Button2_Click(object sender, EventArgs e)
{
sql = "insert into Admin values('" + TextBox2.Text + "','" + TextBox3.Text + "')";//添加语句
string conn = "Server=.;DataBase=Test;uid=sa;pwd=123";
SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(sql, sqlconn);
sqlconn.Open();
cmd.ExecuteReader();
bind("select * from Admin");
}
protected void Button3_Click(object sender, EventArgs e)
{
sql = "update Admin set name='" + TextBox5.Text + "',pwd='" + TextBox6.Text + "' where id=" + Convert.ToInt32(TextBox4.Text);//修改语句
string conn = "Server=.;DataBase=Test;uid=sa;pwd=123";
SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(sql, sqlconn);
sqlconn.Open();
cmd.ExecuteReader();
bind("select * from Admin");
}
算了。代码给出。我这个是SQL得。你改成OLEDB的就能用了。够详细吧!
===========================================================
string conn="Server=服务器名;Database=数据库名;uid=数据库用户名;pwd=数据库密码";
SqlConnection sqlconn=new SqlConnection(conn);//连接到数据库
=========================================================
表的基本操作的语句要SQL的:
添加语句:Insert into 表名 values(字段名1,字段名2)
修改语句:Update 表明
set 字段名1=修改的值,字段名1=修改的值
where 字段名=所修改的记录字段名
删除语句:delete 表名 where 字段名=所要删除字段的名称
查询语句:select * from 表名
=================================================================
例:
数据库名为:Test, 数据库用户名为:sa , 数据库密码:123
有表名为:Admin
该表的字段:id(int)、name(varchar(8))、pwd(varchar(8))
id name pwd
1 aa 123
2 bb 456
3 cc 789
=========================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public string sql;
protected void Page_Load(object sender, EventArgs e)
{
sql = "select * from Admin"; //查询语句
bind(sql);
}
protected void bind(string sql)
{
string conn="Server=.;DataBase=Test;uid=sa;pwd=123";
SqlConnection sqlconn=new SqlConnection(conn);
SqlCommand cmd=new SqlCommand(sql,sqlconn);
sqlconn.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource=dr;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
sql = "delete Admin where id=" + Convert.ToInt32(TextBox1.Text);//删除语句
string conn = "Server=.;DataBase=Test;uid=sa;pwd=123";
SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(sql, sqlconn);
sqlconn.Open();
cmd.ExecuteReader();
bind("select * from Admin");
}
protected void Button2_Click(object sender, EventArgs e)
{
sql = "insert into Admin values('" + TextBox2.Text + "','" + TextBox3.Text + "')";//添加语句
string conn = "Server=.;DataBase=Test;uid=sa;pwd=123";
SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(sql, sqlconn);
sqlconn.Open();
cmd.ExecuteReader();
bind("select * from Admin");
}
protected void Button3_Click(object sender, EventArgs e)
{
sql = "update Admin set name='" + TextBox5.Text + "',pwd='" + TextBox6.Text + "' where id=" + Convert.ToInt32(TextBox4.Text);//修改语句
string conn = "Server=.;DataBase=Test;uid=sa;pwd=123";
SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(sql, sqlconn);
sqlconn.Open();
cmd.ExecuteReader();
bind("select * from Admin");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询