大家好,我是新手,想学习asp.net C#开发, 现在想从最基本的做;“增删改查”。
想知道c# 怎么连接ACCESS 数据
我已经新建了一个“c#网站(空网站)”,目录页就有一个 web.config的文件。
大家能给我一份c#连接access,并且带增删改查的的代码吗? 想学习一下PS: 我在google上找过,都是using ******* public的一些类,这些代码都是放在.cs文件里吧? 那.aspx里假如要select一下列表,要怎么写呢?
谢!!!
想知道c# 怎么连接ACCESS 数据
我已经新建了一个“c#网站(空网站)”,目录页就有一个 web.config的文件。
大家能给我一份c#连接access,并且带增删改查的的代码吗? 想学习一下PS: 我在google上找过,都是using ******* public的一些类,这些代码都是放在.cs文件里吧? 那.aspx里假如要select一下列表,要怎么写呢?
谢!!!
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=数据库文件的绝对地址;);
connection.Open();
MessageBox.Show("打开数据库连接成功");
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=数据库文件的绝对地址;");
connection.Open();
MessageBox.Show("打开数据库连接成功");
2楼的朋友,谢谢了,不过那个说的有点专业,最好简单点,一步一步的告诉我在哪里操作,哎 好难弄。
3楼的朋友,这个是连接的,写到.CS文件里吗? 还有select查询语句怎么写 写在哪个文件里呢
最好一步一步的指点,麻烦大家了,一头雾水啊
下面是一个查询用户名称的例子,你看看吧:using System.Data;
using System.Data.OleDb;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//构造连接字符串
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=数据库文件的绝对地址;" ; OleDbConnection connection = new OleDbConnection(strConnection); //建立连接
connection.Open(); //打开连接
OleDbCommand sqlcmd = new OleDbCommand(@"select * from users where permission='admin'", connection); //查询users表中权限为admin的行
OleDbDataReader reader = sqlcmd.ExecuteReader(); //执行查询
string userName = "";
if (reader.Read())
{
userName = reader["userName"]; //读取字段的值
connection.Close();
reader.Close();
}
connection.Close(); //查询完毕后勿必要关闭连接
this.TextBox1.Text = userName;
}
}
}
谢谢,代码已经写在CS中了,不过我想应用的是 浏览*.aspx文件的时候,就要有数据库读出来的列表.aspx页面中我已经画了表格,想用查询select查出来列表,这页的代码怎么写呢
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\db1.mdb");
//创建command对象并保存sql查询语句
OleDbCommand aCommand = new OleDbCommand("select * from emp_test", aConnection);
try
{
aConnection.Open();
//创建datareader 对象来连接到表单
OleDbDataReader aReader = aCommand.ExecuteReader();
Console.WriteLine("This is the returned data from emp_test table");
//循环遍历数据库
while(aReader.Read())
{
Console.WriteLine(aReader.GetInt32(0).ToString());
}
//关闭reader对象
aReader.Close();
//关闭连接,这很重要
aConnection.Close();
}
//一些通常的异常处理
catch(OleDbException e)
{
Console.WriteLine("Error: {0}", e.Errors[0].Message);
}
}
}
成功运行这个程序的步骤
1.用msaccess创建一个名叫db1.mdb的数据库
2.创建一个名叫emp_test的表单
3.使它包含下列数据域
emp_code int
emp_name text
emp_ext text
4.将上面的代码保存到sample.cs文件中
5.确保数据库位于c:\并确保mdac2.6或是更新的版本已经被安装
6.编译运行
WebConfig里可以判断.
抽象工厂吧我记得
连接字符串成
<add name="accessConnStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source= |DataDirectory|a.mdb" providerName="System.Data.OleDb"/>
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);
}
}
连接字符串大全