我正在制作一个将在多台计算机上安装和运行的应用程序,我的目标是制作一个与该应用程序一起安装的空本地数据库文件,当用户使用该应用程序时,他的数据库将填充来自该应用程序的数据。
您能给我提供以下示例吗:
- 我需要做什么才能让我的应用程序连接到其本地数据库
-
如何使用应用程序中的变量执行查询,例如如何将以下内容添加到数据库中
String abc = "ABC";
String BBB = "Something longer than abc";
等等
编辑 ::我正在使用从“添加>新项目>本地数据库”创建的“本地数据库”,那么我将如何连接到它?抱歉这个愚蠢的问题..我从未在.net中使用过数据库
根据需要,您还可以考虑 Sql CE。我确信,如果您指定了您正在考虑使用的数据库,或者如果您确定您的要求,您将获得连接字符串等的正确且真实的示例。
编辑:这是 SqlCe / Sql Compact 的代码
public void ConnectListAndSaveSQLCompactExample()
{
// Create a connection to the file datafile.sdf in the program folder
string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\datafile.sdf";
SqlCeConnection connection = new SqlCeConnection("datasource=" + dbfile);
// Read all rows from the table test_table into a dataset (note, the adapter automatically opens the connection)
SqlCeDataAdapter adapter = new SqlCeDataAdapter("select * from test_table", connection);
DataSet data = new DataSet();
adapter.Fill(data);
// Add a row to the test_table (assume that table consists of a text column)
data.Tables[0].Rows.Add(new object[] { "New row added by code" });
// Save data back to the databasefile
adapter.Update(data);
// Close
connection.Close();
}
记得添加对System.Data.SqlServerCe的引用
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)