在网上看过了很多文章,也在此论坛上搜索过了,得到的答案总解决不了此问题,希望牛人帮忙解答.
问题如下:
条件:已在项目上添加引用了System.web
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data.OleDb;namespace sql
{
class Program
{
static void Main(string[] args)
{
try
{
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
connstring += System.Web.HttpContext.Current.Server.MapPath("winner.mdb");
string sql = "select * from [user]"; OleDbConnection conn = new OleDbConnection(connstring);
conn.Open(); OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader read = cmd.ExecuteReader();
while (read.Read())
{
Console.WriteLine("{0}...", read[0].ToString());
}
read.Close();
conn.Close();
}
catch (Exception e) { Console.WriteLine("连接数据库众所失败!"+e.Message); }
Console.ReadLine();
}
}
}在connstring += System.Web.HttpContext.Current.Server.MapPath("winner.mdb")这句设置断点,
得到的异常是$exception {"未将对象引用设置到对象的实例。"} System.Exception {System.NullReferenceException}如果我将相对路径改为绝对路径:
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
connstring +=@"F:\C#练习\sql\Data\winner.mdb"时,运行正确.请问问题何在???
问题如下:
条件:已在项目上添加引用了System.web
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data.OleDb;namespace sql
{
class Program
{
static void Main(string[] args)
{
try
{
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
connstring += System.Web.HttpContext.Current.Server.MapPath("winner.mdb");
string sql = "select * from [user]"; OleDbConnection conn = new OleDbConnection(connstring);
conn.Open(); OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader read = cmd.ExecuteReader();
while (read.Read())
{
Console.WriteLine("{0}...", read[0].ToString());
}
read.Close();
conn.Close();
}
catch (Exception e) { Console.WriteLine("连接数据库众所失败!"+e.Message); }
Console.ReadLine();
}
}
}在connstring += System.Web.HttpContext.Current.Server.MapPath("winner.mdb")这句设置断点,
得到的异常是$exception {"未将对象引用设置到对象的实例。"} System.Exception {System.NullReferenceException}如果我将相对路径改为绝对路径:
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
connstring +=@"F:\C#练习\sql\Data\winner.mdb"时,运行正确.请问问题何在???
解决方案 »
- VS2008 C#程序合并问题
- 反射调用 引起 IIS进程内存无法释放的问题
- 如何获取richtextbox里当前光标在该控件里的位置
- WinForm下DataGridView编辑状态下删除最后一行问题
- Environment.GetCommandLineArgs怎么使用才能在Winform里获得启动参数
- 控件如何截获Form的Click事件?
- 请教一个问题获取其他网站内容的问题
- VSS在XP中能装吗
- 谁有C#的DataGrid经典例子!!!
- 我用C#开发的的一网站,有一页它的显示是乱码(UTF-8)但其它的正常
- 自定义控件 运行时输入问题?
- SQL存储过程中加多个参数出现错误提示:参数过多?我有8个输入参数和一个输出参数?如何解决?
改成
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
connstring +="\""+System.Web.HttpContext.Current.Server.MapPath("winner.mdb")+"\"";
string sql = "select * from [user]";
Application.StartupPath + Path.DirectorySeparatorChar + fileName;
这里的Application.StartupPath就是程序的根目录了,fileName是文件名。
否则只能用绝对路径
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+app.path+"你的文件夹\..\文件";
而Server.MapPath提供的路径是给b/s型的,就是网站的。因为网站的站的,跟服务器的主目录相关,不让它是本地主机相关,采用Server.Mappath
一为了数据安全。二也方便访问。
后来他说把数据库放在该程序下的bin\Debug目录下,
或者在项目->sql属性->调试,在工作目录(K)选择路径也行,不过此方法有点像绝对路径
经试验通过.
接分...