点滴积累,融会贯通

-----喜欢一切有兴趣的东西

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

连接 SQL Server CE 数据库

 

string sPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
            SqlCeConnection conn 
= new SqlCeConnection("Data Source=" + sPath + "\\TestDB.sdf;Password ='123456'");
            SqlCeDataAdapter da 
= new SqlCeDataAdapter("select * from testTb",conn);
            DataTable dt 
= new DataTable();
            conn.Open();
            da.Fill(dt);
            conn.Close();
            
if (dt != null && dt.Rows.Count > 0)
            {
                MessageBox.Show(dt.Rows[
0][1].ToString());
            }

 

 sPath是得到数据的绝对地址,将数据库sdf文件和可执行exe程序放在同一目录下即可

如果是远程数据库则

SqlConnection con = new SqlConnection("server=192.168.1.179;database=TestDB;uid=sa;pwd=123;");

 

上面的服务器地址必须写真实的网络IP地址,不能为localhost或127.0.0.1

posted on 2010-04-13 13:25  小寒  阅读(369)  评论(0编辑  收藏  举报