SQL Server CE 本地应用程序问题
我来这里是为了解决 C# 应用程序中 SQL Server CE 的问题。
这是一个非常简单的问题,起初我试图在表中执行 INSERT
,但它没有做到,所以我搜索了,解决方案是将文字字符串连接到数据库。
try
{
cnTrupp.Open();
SqlCeCommand com = new SqlCeCommand("INSERT INTO tipo_venta(nombre) VALUES (@nombre)", cnTrupp);
com.Parameters.AddWithValue("@nombre", pNombre);
com.ExecuteNonQuery();
com.Dispose();
}
catch (SqlCeException e)
{
LogFile log = new LogFile(e.Message);
}
finally
{
cnTrupp.Close();
}
之后,使用文字字符串,我想知道,当我部署应用程序时,我应该如何更改该连接字符串?所以它指向新计算机中的实际数据库
I'm here for a trouble with SQL Server CE in a C# application.
This is a really simple question, at first I was trying to do an INSERT
into a table, but it didn't do it, so I searched and the solution was to put the literal string to connect to the database.
try
{
cnTrupp.Open();
SqlCeCommand com = new SqlCeCommand("INSERT INTO tipo_venta(nombre) VALUES (@nombre)", cnTrupp);
com.Parameters.AddWithValue("@nombre", pNombre);
com.ExecuteNonQuery();
com.Dispose();
}
catch (SqlCeException e)
{
LogFile log = new LogFile(e.Message);
}
finally
{
cnTrupp.Close();
}
After that with the literal string, I was wondering, when I deploy the app, how I'm supposed to change that connection string? so it points to the actual database in the new computer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“Paul Sasik”帖子中的评论讨论了应用程序的
app.config
文件中的Data Source=|DataDirectory|\example.sdf
条目。为了完整起见:此
|DataDirectory|
部分是一个宏,会自动扩展到应用程序运行的文件夹,不应进行硬编码。如果要更改文件夹,可以在Program.cs
中使用以下行:AppDomain.CurrentDomain.SetData("DataDirectory",);
至少对于桌面应用程序来说是这样。由于移动应用程序(在 VS 2005 和 2008 中)不支持相同的配置机制,因此您必须在那里手动创建连接字符串。
The comments on "Paul Sasik"'s post talk about the
Data Source=|DataDirectory|\example.sdf
entry in theapp.config
file of your application.For the sake of completeness: This
|DataDirectory|
part is a macro that expands automatically to the folder where your application is running and should not be hardcoded. If you want to change the folder, you may use the following line inProgram.cs
:AppDomain.CurrentDomain.SetData("DataDirectory", <New Folder>);
At least this is true for desktop applications. As mobile applications (in VS 2005 and 2008) don't support the same configuration mechanism, you have to create the connection string manually there.
使用 .NET app.config 文件。这是一篇 VB 文章,可帮助您入门。
.NET 配置文件相当容易使用,但对于数据库连接等信息,您可能需要考虑加密配置文件中的字符串。或者至少是密码。也就是说,如果安全是一个问题。很多时候并非如此。特别是在本质上不安全的移动设备上(至少在 WinCE 世界中......最高可达 CE5 v.6)
Make use of the .NET app.config file. This is a VB article but will get you started.
.NET config files are fairly easy to work with but with information like a db connection, you might want to consider encrypting the string in the config file. Or at least the password. That is if security is a concern. Many times it's not. Especially on mobile devices which are inherently unsecure (at least in the WinCE world... up to CE5 v.6)