我正在制作一个将在多台计算机上安装并运行的应用程序,我的目标是创建一个随应用程序一起安装的空本地数据库文件,当用户使用该应用程序时,他的数据库将填充应用程序中的数据.你能给我提供以下例子:
如何使用应用程序中的变量执行查询,例如,如何向数据库添加以下内容
String abc = "ABC";
String BBB = "Something longer than abc";
Run Code Online (Sandbox Code Playgroud)编辑:: 我正在使用从"添加>新项目>本地数据库"创建的"本地数据库",那么我将如何连接到该?抱歉愚蠢的问题..我从未在.net中使用过数据库
sin*_*e j 22
根据需要,您也可以考虑使用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();
}
Run Code Online (Sandbox Code Playgroud)
请记住添加对System.Data.SqlServerCe的引用
STW*_*STW 11
我没有看到任何人建议SQL Compact; 它与SQLite类似,因为它不需要安装和定制到低端数据库.它源于SQL Mobile,因此占用空间小,功能集有限,但如果您熟悉Microsoft的SQL产品,它应该有一些熟悉程度.
SQL Express是另一种选择,但要注意它需要独立安装,并且比applciation的本地缓存需要的更强一些.这说它也比SQL Compact或SQLite强大得多.
| 归档时间: |
|
| 查看次数: |
69420 次 |
| 最近记录: |