连接 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());
}
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