c#查询access数据库代码 请问怎样在c#项目中查询access数据库中的数据,代码是怎样啊??请高手指点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考以下代码,看能否解决到你的问题string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";strConnection+=@"Data Source=MapPath("数据库.mdb");//这里是相对路径OleDbConnection objConnection=new OleDbConnection(strConnection);objConnection.open(); string strSQL="SELECT * FROM Table"; OleDbCommand Comm = new OleDbCommand(strSQL, objConnection); OleDbDataReader adr = Comm.ExecuteReader(); while(adr.Read()) { 变量 = adr["字段名"].ToString(); 变量 = adr["字段名"].ToString(); } objConnection.close(); <add name="xkConnStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\XKArticle.mdb" providerName="System.Data.OleDb"/> 用access操作类public static string connectionString = "";public static OleDbDataReader ExecuteReader(string strSQL) { OleDbConnection connection = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand(strSQL, connection); try { connection.Open(); OleDbDataReader myReader = cmd.ExecuteReader(); return myReader; } catch (System.Data.OleDb.OleDbException e) { throw new Exception(e.Message); } } public static DataSet Query(string SQLString) { using (OleDbConnection connection = new OleDbConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.OleDb.OleDbException ex) { throw new Exception(ex.Message); } return ds; } } 参考以下代码,看能否解决到你的问题string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";strConnection+=@"Data Source=MapPath("数据库.mdb");//这里是相对路径OleDbConnection objConnection=new OleDbConnection(strConnection);objConnection.open();string strSQL="SELECT * FROM Table";OleDbCommand Comm = new OleDbCommand(strSQL, objConnection);OleDbDataReader adr = Comm.ExecuteReader(); while(adr.Read()) { 变量 = adr.GetValue(0).ToString(); 变量 = adr.GetValue(1).ToString(); . . }objConnection.close(); private void Form1_Load(object sender, EventArgs e) { string datacon = global::accesscon.Properties.Settings.Default.db1ConnectionString1; string sqldatacon = "select count(*) from stu_info "; OleDbConnection olecon = new OleDbConnection(datacon); olecon.Open(); OleDbCommand olecom = new OleDbCommand(sqldatacon, olecon); textBox1.Text = olecom.ExecuteScalar().ToString(); ; } OleDbConnection cnn=new OleDbConnection(Provider=Microsoft.Jet.OleDb.4.0; Data Source=MapPath("数据库名.mdb)); cnn.open(); Oledbcommand cmm=new Oledbcommand();cmm.connection=cnn;cmm.commandtype=commandtype.text;cmm.commandtext="SELECT * FROM 表名"; OleDbDataReader adr = Cmm.ExecuteReader(); while(adr.Read()) { 控件.text = adr["字段名"].ToString(); 控件.text= adr["字段名"].ToString(); } cnn.close(); 看看这个方法。 private void name_SelectedIndexChanged(object sender, EventArgs e) { string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\")); reportPath += @"\\money.mdb"; string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath; //创建OLEDB连接对象 OleDbConnection con = new OleDbConnection(ConStr); con.Open(); OleDbCommand cmd = new OleDbCommand("select * from user where uname=" + this.name.Text + "", con); OleDbDataReader adr = cmd.ExecuteReader(); if (adr.Read()) { this.res.Text = adr.GetValue(0).ToString(); u_res = this.res.Text; }我的选择下拉表代码如上,为什么运行后我选择后会说FROM子句语法错误啊。。我的目的是想获取管理员权限请高手指点。谢谢 ("select * from user where uname=" + this.name.Text + "", con); 有错误写成这样子就行了("select * from user where uname= +'" + this.name.Text + "'+", con); ("select * from user where uname= +'" + this.name.Text + "'+", con); 再不行把两个红色+去掉,'" + this.name.Text + "' 一个要单引号 请大家看看我这个窗体的代码,给我指点一下。还是From子句错误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 moneySYS{ public partial class frmLogin : Form { private string u_name; //获取用户名 private string u_pwd; //获取密码 private string u_res; //获取权限 public frmLogin() { InitializeComponent(); } private void frmLogin_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“moneyDataSet.user”中。您可以根据需要移动或移除它。 this.userTableAdapter.Fill(this.moneyDataSet.user); } private void enter_Click(object sender, EventArgs e) { string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\")); reportPath += @"\\money.mdb"; string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath; //创建OLEDB连接对象 OleDbConnection con = new OleDbConnection(ConStr); con.Open(); if (con.State == ConnectionState.Open) { OleDbCommand ole = new OleDbCommand("select pwd from user where uname='"+name.Text+"'"); welcome wle = new welcome(); this.Hide(); wle.Show(); } } private void exit_Click(object sender, EventArgs e) { Application.Exit(); } private void name_SelectedIndexChanged(object sender, EventArgs e) { string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\")); reportPath += @"\\money.mdb"; string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath; //创建OLEDB连接对象 OleDbConnection con = new OleDbConnection(ConStr); con.Open(); OleDbCommand cmd = new OleDbCommand("select * from user where uname=+'"+ this.name.Text +"'+",con); OleDbDataReader adr = cmd.ExecuteReader(); if (adr.Read()) { this.res.Text = adr["rmks"].ToString(); u_res = this.res.Text; } } }} 我正在使用的:using System.Data.OleDb;protected void Page_Load(object sender, EventArgs e) {//数据库是在App_Data下的studentinfo.mdb string tabel_name = "test";//数据表名称 OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|\\studentinfo.mdb"); conn.Open(); OleDbDataAdapter da = new OleDbDataAdapter("select * from "+tabel_name+" order by id asc", conn);//desc降序 asc升序 OleDbCommandBuilder cmd = new OleDbCommandBuilder(da); DataSet ds = new DataSet(); da.Fill(ds, "basic_info"); GridView1.DataSource = ds; GridView1.DataBind(); conn.Close(); }我的测试站:http://nz.oicp.net:808 select * from userselect * from uuserselect * from uuuserselect * from uuuuserselect * from uuuuuserselect * from uuuuuuser关键字!!!!!!!!!!!! 获取tabpage中的控件信息 追踪分析的问题 请教类似lock方法的问题 用C#制作简单的计算器 DataRow:如何动态的添加字段? 一个简单的问题,关于webbrowser 在Winform项目中,怎样确定编号的唯一并设置位数规则 怎么修改这XML 如何把excel中的部分行的数据导入到SQL中 上传图片 WebStream1.ReadTimeout”引发了“System.InvalidOperationException”类型的异常 这样的需求该如何实现?高分求代码示例 关于.NET 未将对象引用设置到对象的实例
strConnection+=@"Data Source=MapPath("数据库.mdb");//这里是相对路径OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection.open(); string strSQL="SELECT * FROM Table";
OleDbCommand Comm = new OleDbCommand(strSQL, objConnection); OleDbDataReader adr = Comm.ExecuteReader(); while(adr.Read())
{
变量 = adr["字段名"].ToString();
变量 = adr["字段名"].ToString();
} objConnection.close();
public static string connectionString = "";
public static OleDbDataReader ExecuteReader(string strSQL)
{
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(strSQL, connection);
try
{
connection.Open();
OleDbDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.OleDb.OleDbException e)
{
throw new Exception(e.Message);
}
}
public static DataSet Query(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.OleDb.OleDbException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
strConnection+=@"Data Source=MapPath("数据库.mdb");//这里是相对路径OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection.open();string strSQL="SELECT * FROM Table";
OleDbCommand Comm = new OleDbCommand(strSQL, objConnection);OleDbDataReader adr = Comm.ExecuteReader(); while(adr.Read())
{
变量 = adr.GetValue(0).ToString();
变量 = adr.GetValue(1).ToString();
.
.
}objConnection.close();
{
string datacon = global::accesscon.Properties.Settings.Default.db1ConnectionString1;
string sqldatacon = "select count(*) from stu_info ";
OleDbConnection olecon = new OleDbConnection(datacon);
olecon.Open();
OleDbCommand olecom = new OleDbCommand(sqldatacon, olecon);
textBox1.Text = olecom.ExecuteScalar().ToString(); ;
}
Data Source=MapPath("数据库名.mdb));
cnn.open();
Oledbcommand cmm=new Oledbcommand();
cmm.connection=cnn;
cmm.commandtype=commandtype.text;
cmm.commandtext="SELECT * FROM 表名";
OleDbDataReader adr = Cmm.ExecuteReader(); while(adr.Read())
{
控件.text = adr["字段名"].ToString();
控件.text= adr["字段名"].ToString();
} cnn.close();
看看这个方法。
{
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\\money.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
//创建OLEDB连接对象
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from user where uname=" + this.name.Text + "", con);
OleDbDataReader adr = cmd.ExecuteReader();
if (adr.Read())
{
this.res.Text = adr.GetValue(0).ToString();
u_res = this.res.Text; }我的选择下拉表代码如上,为什么运行后我选择后会说FROM子句语法错误啊。。
我的目的是想获取管理员权限请高手指点。谢谢
有错误
写成这样子就行了
("select * from user where uname= +'" + this.name.Text + "'+", con);
再不行把两个红色+去掉,'" + this.name.Text + "' 一个要单引号
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 moneySYS
{
public partial class frmLogin : Form
{
private string u_name; //获取用户名
private string u_pwd; //获取密码
private string u_res; //获取权限
public frmLogin()
{
InitializeComponent();
} private void frmLogin_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“moneyDataSet.user”中。您可以根据需要移动或移除它。
this.userTableAdapter.Fill(this.moneyDataSet.user);
} private void enter_Click(object sender, EventArgs e)
{
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\\money.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
//创建OLEDB连接对象
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
if (con.State == ConnectionState.Open)
{
OleDbCommand ole = new OleDbCommand("select pwd from user where uname='"+name.Text+"'");
welcome wle = new welcome();
this.Hide();
wle.Show();
} } private void exit_Click(object sender, EventArgs e)
{
Application.Exit();
} private void name_SelectedIndexChanged(object sender, EventArgs e)
{
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\\money.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
//创建OLEDB连接对象
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from user where uname=+'"+ this.name.Text +"'+",con);
OleDbDataReader adr = cmd.ExecuteReader();
if (adr.Read())
{
this.res.Text = adr["rmks"].ToString();
u_res = this.res.Text; }
}
}
}
using System.Data.OleDb;protected void Page_Load(object sender, EventArgs e)
{//数据库是在App_Data下的studentinfo.mdb
string tabel_name = "test";//数据表名称
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|\\studentinfo.mdb");
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from "+tabel_name+" order by id asc", conn);//desc降序 asc升序
OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "basic_info");
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}我的测试站:http://nz.oicp.net:808
select * from uuser
select * from uuuser
select * from uuuuser
select * from uuuuuser
select * from uuuuuuser
关键字!!!!!!!!!!!!