这样的数据库连接怎么写 取当前程序集的路径,然后加上“\database\access.mdb”就可以了,需要一点点反射技巧。Type.Assembly.Location 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用相对路径Server.MapPath(@"database\access.mdb"); 这样写不行说是找不到名称空间Server孟老这好像是WEB里的写法吧 public String ConnectAccess(String dbf,String psw) // 产生Access连接串 { return("Provider=\"Microsoft.Jet.OLEDB.4.0\";" + "Jet OLEDB:Database Password="+psw+";" + "Data Source=\""+dbf+"\";" + "Password=;"); }public String DbfPath(String dbf) // 从当前目录往根目录找文件 dbf { String path=Application.StartupPath; bool root=false; // 是否根目录 do { String file1=path; root=(path[path.Length-1]=='\\'); if (!root) file1+="\\"; file1+=dbf; if (File.Exists(file1)) return(file1); if (!root) path=Directory.GetParent(path).FullName; } while (!root); return(""); }private void Form1_Load(object sender, System.EventArgs e) { String dbf0="dagl.mdb"; String dbf1=DbfPath(dbf0); if (dbf1=="") { MessageBox.Show("文件: "+dbf0+" 不存在!"); Application.Exit(); return; } oleDbConnection1.ConnectionString=ConnectAccess(dbf1,""); oleDbConnection1.Open(); 只要数据库的路径和应用程序的路径一样,或者数据库的路径与应用程序在同一根目录下。可以考虑用: [VB.NET] Dim currPath As String = System.Environment.CurrentDirectory Dim dbPath As String = currPath & "\" & DBNAME [C#] string currPath = System.Environment.CurrentDirectory; string dbPath = currPath + "\\" + DBNAME; 把数据库和你生成的exe放在一起!就是放在Debug目录下在连接字符串里直接写数据库名称不用写路径!:) string DataAccessPath;DataAccessPath=Application.s.StartupPath; string DataAccessPath;DataAccessPath=Application.StartupPath; 使用配制文件,写在Web.Config或app.Config中 winfrom里面没有Server.MapPath吧? WinForm中如何用代码控制生成图片 问大家个基础的问题.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 请教一个C#基础问题 c#线性表的语法,有点看不明,高手进来指法下! 求个C#网络通讯程序源码 很久没有散分了,小问题,大散分(函数) 找不到DLL如何解决 问个效率问题 很头痛! vs2005 中的数据库问题 使用NPOI导入Excel遇到的问题 [高兴发分]技术贴情不要删除. 有人遇到这样的问题吗?
Server.MapPath(@"database\access.mdb");
这样写不行说是找不到名称空间Server孟老这好像是WEB里的写法吧
{
return("Provider=\"Microsoft.Jet.OLEDB.4.0\";" +
"Jet OLEDB:Database Password="+psw+";" +
"Data Source=\""+dbf+"\";" +
"Password=;");
}public String DbfPath(String dbf) // 从当前目录往根目录找文件 dbf
{
String path=Application.StartupPath;
bool root=false; // 是否根目录
do
{
String file1=path;
root=(path[path.Length-1]=='\\');
if (!root)
file1+="\\";
file1+=dbf;
if (File.Exists(file1))
return(file1);
if (!root)
path=Directory.GetParent(path).FullName;
} while (!root);
return("");
}private void Form1_Load(object sender, System.EventArgs e)
{
String dbf0="dagl.mdb";
String dbf1=DbfPath(dbf0);
if (dbf1=="")
{
MessageBox.Show("文件: "+dbf0+" 不存在!");
Application.Exit();
return;
}
oleDbConnection1.ConnectionString=ConnectAccess(dbf1,"");
oleDbConnection1.Open();
可以考虑用:
[VB.NET]
Dim currPath As String = System.Environment.CurrentDirectory
Dim dbPath As String = currPath & "\" & DBNAME
[C#]
string currPath = System.Environment.CurrentDirectory;
string dbPath = currPath + "\\" + DBNAME;
就是放在Debug目录下在连接字符串里直接写数据库名称不用写路径!:)
DataAccessPath=Application.s.StartupPath;
DataAccessPath=Application.StartupPath;