如何使用本地数据库在类中设置连接字符串? [英] how to set up connection string in a class using local database?

查看:101
本文介绍了如何使用本地数据库在类中设置连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i我正在从youtube的教程中创建一个图书管理项目

并且那个人使用vb语言而我正试图在c#winform中创建

现在那家伙使用本地数据库,其格式为.sdf,我添加.mdf本地数据库,因为我使用的是visual studio 2013



然后他创建了一个数据库-class里面他定义连接字符串和其他数据库相关的代码



现在他用这个

in vb

public CLass DBMSClass

pivate DBMSConnectionString =他从数据库属性.sdf复制的内部连接字符串

结束类

现在我尝试创建完全像它是什么

类DBMSCLASS

{

//定义连接字符串

私有字符串DBMSConnectionString;



DBMSConnectionString =DataSource(LocalDB)\ v11.0; AttachDbFilename =C:\\Users\\user-name \ \documents \\visual studio 2013 \\Projects \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b $ b}

现在我收到来自c:\\到security = true的错误);这行是红色但这不是红色DataSource(LocalDB)\ v11.0; AttachDbFilename

所以有人可以帮我解决它的错误。





参考视频链接 https://www.youtube.com/watch ?v = 3qXLn7ZrllQ 你可以在20:01分看到视频

hi i am making a project of library management from a tutorial in youtube
and that guy use vb language and i am trying to make it in c# winform
now that guy use local data base and its format is .sdf and i add .mdf local database because i am using visual studio 2013

then he made a database-class in it he define connection string and other database related code

now he use this
in vb
public CLass DBMSClass
pivate DBMSConnectionString="inside connection string which he copy from database properties .sdf"
End Class
now i try to create exactly like it which is
class DBMSCLASS
{
//define connection string
private string DBMSConnectionString;

DBMSConnectionString="DataSource(LocalDB)\v11.0;AttachDbFilename="C:\\Users\\user-name\\documents\\visual studio 2013\\Projects\\library mangement system\\library mangement system\\LMS.mdf";Integrated Security=True");
}
now i am getting error from c:\\ to security=true"); this line are red but this is not red DataSource(LocalDB)\v11.0;AttachDbFilename
so someone can help me what is wrong in it.


for refernce video link https://www.youtube.com/watch?v=3qXLn7ZrllQ you can see at 20:01 minute of video

推荐答案

好的,这里有几个问题:

1)SDF不是VB特定的:如果是任何.NET应用程序都可以使用的数据库格式,只要它引用了SQLCE程序集即可。 MDF文件是Access数据库,需要在代码中使用不同的类,以及安装合适的ACE或JET驱动程序 - 除非您使用SQL Server并以此方式附加DB。即使这样,你也需要SQLConnection,SQLCommand等类似的对象来访问它。

2)你不能只写这样的连接字符串!

OK, there are a couple of problems here:
1) SDF is not VB specific: if's a database format which can be used by any .NET application, provided it has the SQLCE assemblies referenced. MDF files are Access databases and need different classes in your code, and well as a suitable ACE or JET driver installed - unless you are using SQL Server and attaching the DB that way. Even then, you will need SQLConnection, SQLCommand and suchlike objects to access it.
2) You can't just write a connection string like that!
DBMSConnectionString="DataSource(LocalDB)\v11.0;AttachDbFilename="C:\\Users\\user-name\\documents\\visual studio 2013\\Projects\\library mangement system\\library mangement system\\LMS.mdf";Integrated Security=True");

我不是正确的我知道你在那里尝试做什么,但 可能 工作得更好:

I'm not possitive I know what you re trying to do there, but this might work a little better:

DBMSConnectionString="DataSource(LocalDB)\v11.0;AttachDbFilename=\"C:\\Users\\user-name\\documents\\visual studio 2013\\Projects\\library mangement system\\library mangement system\\LMS.mdf\";Integrated Security=True");





但我首先要在VS中创建一个连接并将其建立在那里的连接字符串上:

1)打开Server Explorer窗格。

2)右键单击数据连接并选择添加连接

3)在随后的对话框中,选择您的数据源和数据库,指定安全信息,然后按测试连接按钮。

4)当连接正常时,按确定

5)在服务器资源管理器窗格中突出显示数据库,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中。



因为您每次都附加文件(为什么?)你可能需要调整它。



But I'd start by creating a connection in VS and base it on the connections string from there:
1) Open the Server Explorer pane.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

Since you are attaching the file each time (why?) you may have to tweak this.


这篇关于如何使用本地数据库在类中设置连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆