使用GetDirectoryName连接到本地数据库 [英] connect to local database with GetDirectoryName

查看:86
本文介绍了使用GetDirectoryName连接到本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  dataGridView2_CellContentClick( object  sender,DataGridViewCellEventArgs e)
{
SqlCeConnection con = new SqlCeConnection( 数据源=
+ System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly( ).Location), MyDB.sdf));
SqlCeDataAdapter sda = new SqlCeDataAdapter();
SqlCeCommand cmd = con.CreateCommand();
cmd.CommandText = select * from nolon;
sda.SelectCommand = cmd;
SqlCeDataAdapter dad = new SqlCeDataAdapter(cmd);
DataSet ds = new DataSet();
dad.Fill(ds);

if (ds.Tables [ 0 ]。Rows.Count > 0
{


dataGridView1.DataSource = ds;
}
}



datagridview没有显示任何数据甚至列名

解决方案

< blockquote>要检查的事项:



- bin \debug文件夹中数据库的副本肯定在表nolon中有一些数据(或者如果你运行它作为一个Exe而不是在Visual Studio中然后检查bin \ release文件夹)



- 数据库不需要用户ID和密码(如果它包含这些在您的连接字符串中)



- 将断点置于if(ds.Tables [0] .Rows.Count> 0)并查看Rows的值.Count是。



- 如果是> 0然后看看这个 MS论坛发帖 [ ^ ]



- 如果程序没有在断点处停止那么它会抛出一个异常你没有看到 - 尝试将代码包装在try-catch块中以捕获和查看异常


请尝试此连接字符串



 SqlConnection conn = new SqlConnection 
{
ConnectionString =Data Source =+ System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()。GetName( ).CodeBase)+\\Database.sdf
};





或者你使用相对路径为下面



 Connec tionString =Data Source = | DataDirectory | \Database.sdf; 





将DataDirectory修改为可执行文件的路径:



 string executable = System.Reflection.Assembly.GetExecutingAssembly()。Location; 
string path =(System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData(DataDirectory,path);


private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            SqlCeConnection con = new SqlCeConnection("Data Source="
                + System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "MyDB.sdf"));
            SqlCeDataAdapter sda = new SqlCeDataAdapter();
            SqlCeCommand cmd = con.CreateCommand();
            cmd.CommandText = "select * from nolon";
            sda.SelectCommand = cmd;
            SqlCeDataAdapter dad=new SqlCeDataAdapter(cmd);
            DataSet ds = new DataSet();
            dad.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                
               
                dataGridView1.DataSource = ds;
            }
        }


datagridview did not show any data or even column name

解决方案

Things to check:

- the copy of the database in your bin\debug folder definitely has some data in table nolon (or if you are running this as an Exe and not in Visual Studio then check the bin\release folder)

- the database does not require a user-id and password (if it does then include these in your connection string)

- put break point on if(ds.Tables[0].Rows.Count > 0) and see what the value of Rows.Count is.

- If it's > 0 then have a look at this MS forums post[^]

- If the program doesn't stop at the breakpoint then it's throwing an exception which you are not seeing - try wrapping the code in a try-catch block to capture and view the exception


Please try this connection string

SqlConnection conn = new SqlConnection
{
    ConnectionString = "Data Source=" +    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Database.sdf"
};



alternatively you are use Relative path as below

ConnectionString = "Data Source=|DataDirectory|\Database.sdf";



Modifying DataDirectory as executable's path:

string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
        string path = (System.IO.Path.GetDirectoryName(executable));
        AppDomain.CurrentDomain.SetData("DataDirectory", path);


这篇关于使用GetDirectoryName连接到本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆