C#winForm中连接Access数据库的问题!!!!
privatevoidForm1_Load(objectsender,EventArgse){stringstrPath=Application.StartupPath+...
private void Form1_Load(object sender, EventArgs e)
{
string strPath = Application.StartupPath + "db_09.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strPath;
//string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + "C:\\Users\\lenovo\\Desktop\\db_09.mdb";
OleDbConnection oleCon = new OleDbConnection(ConStr);
OleDbDataAdapter oleDap = new OleDbDataAdapter("select*from 账目",oleCon);
DataSet ds = new DataSet();
oleDap.Fill(ds, "账目");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
oleCon.Dispose();
}
为啥我这段代码什么都显示不出来。。我是新手。。才开始学。。学不明白。。求指教 展开
{
string strPath = Application.StartupPath + "db_09.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strPath;
//string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + "C:\\Users\\lenovo\\Desktop\\db_09.mdb";
OleDbConnection oleCon = new OleDbConnection(ConStr);
OleDbDataAdapter oleDap = new OleDbDataAdapter("select*from 账目",oleCon);
DataSet ds = new DataSet();
oleDap.Fill(ds, "账目");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
oleCon.Dispose();
}
为啥我这段代码什么都显示不出来。。我是新手。。才开始学。。学不明白。。求指教 展开
4个回答
展开全部
using System;
namespace StudentContactSoft
{
public static class AccessHelper
{
public static string strCon =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "\\data\\ClientData.mdb;Persist Security Info = True";
///通过传入的SQL查询语句获取数据集
public static DataSet GetDataSet(string strsql)
{
DataSet ds = new DataSet();
try
{
using (OleDbConnection con = new OleDbConnection(strCon))
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
OleDbDataAdpter adapter - new OleDbDataAdapter(strsql, con);
adapter.Fill(ds);
return ds;
}
}
catch (OleDbException odbException)
{
throw odbException;
}
}
///返回受影响的行数,常用以更新,插入,删除等功能
public static int ExecuteNonquery(string sql)
{
try
{
using (OleDbConnection con = new OleDbConnection(strCon)
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
OleDbCommand cmd = new OleDbCommand();
cmd.Connection =con;
cmd.CommandText = sql;
OleDbTransaction ts = con.BeginTransaction();
cmd.Transaction = ts;
int h = cmd.ExecuteNonQuery();
if(h>0)
{
ts.commit();
}
else
{
ts.Rollback();
}
return h;
}
}
catch (OleDbException ex)
{
throw ex
}
}
///返回第一行第一列的值
public static object ExcuteScare(string sql)
{
try
{
using (OleDbConnection con = new OleDbConnection(strCon))
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
using (OleDbCommand cmd = new OleDbCommand(sql,con))
{
return cmd.ExecuteScalar();
}
}
}
catch (OleDbException odbException)
{
throw odbException;
}
}
}
展开全部
有三个地方需注意:
1、没有数据库的登录账号和密码。
2.少了一句 oleCon.open() 来打开连接
3.最好用try-catch 来捕捉异常
1、没有数据库的登录账号和密码。
2.少了一句 oleCon.open() 来打开连接
3.最好用try-catch 来捕捉异常
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你应该是少了一个路径符:
string strPath = Application.StartupPath + "\\db_09.mdb";
string strPath = Application.StartupPath + "\\db_09.mdb";
追问
我加上了。。还是什么都显示不出来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
oleCon.Open();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询