在 vb.net 中为通过 Visual Studio 2010 创建的本地数据库设置 SqlConnection 字符串 [英] Setting Up SqlConnection string in vb.net for a local database created through visual studio 2010
问题描述
我已经搜索了几个小时,发现了几个问题,但没有一个能以我能理解的方式真正解释这一点.
I have been searching for a couple hours, and found several questions, but none of them really explained this in a way I can understand.
我正在编写一个类似于 Rock Paper Sissors 的游戏,但有更多的选择,并且可能出现平局.我最初对所有可能的结果进行了硬编码,然后决定尝试一个数据库,这样我也可以学习和练习 sql.
I'm programming a game similar to Rock Paper Sissors, except with many more selections, and the possiblity of a tie. I had originally hardcoded all of the possible outcomes, then decided to try a database so I can learn and practice sql as well.
问题是,我一生都无法弄清楚如何连接到我的本地数据库,现在我已经通过 Visual Studio 2010 设置并填充了它.我可以通过服务器资源管理器连接到它就好了,我可以在解决方案资源管理器中看到它.我已经尝试了几件事,大致如下:
Problem is, I can't figure out for the life of me how to connect to my local database, now that I have set it up and filled it through Visual Studio 2010. I can connect to it through Server Explorer just fine, and I can see it in Solution Explorer. I've tried several things, along the lines of:
Private sqlConn As New SqlConnection("Data Source=(local)|DataDirectory|\Outcomes.sdf;database=Outcomes;Integrated Security=true")
Private sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS; Integrated Security=true")
Dim sqlConn As SqlConnection
sqlConn = New SqlConnection("DataSource=..\..\Outcomes.sdf")
我对 sql 比较陌生,但通过修改构建 sql 语句并得到我想要的结果,我已经足够了解了.但我以前从未连接到数据库.我查看了 MSDN 并尝试了我在那里看到的一些东西(所有看起来都是我需要的东西,真的)但它仍然没有奏效.
I am relatively new to sql, but know enough through tinkering to build a sql statement and get me the result I want. But I've never connected to a database before. I've looked on MSDN and tried several things I saw on there (everything that looked like what I needed, really) but it still hasn't worked.
如果我可以连接,我已经设置了我的语句,并且已经通过数据库本身对其进行了测试.任何帮助都会很棒,特别是如果它以我可以理解的方式进行解释并供以后使用.
If I can connect, I already have my statement set, and have tested it through the database itself. Any help would be wonderful, especially if it's explained in a way I can understand it and use it for later.
另外,如果它有帮助并且没有被我尝试过的代码注意到,我的数据库名称是 Outcomes
.我不知道这是否需要或会有所帮助,但以防万一.
Also, if it helps and isn't noticed through my tried code, my db name is Outcomes
. I don't know if that is needed or will help, but just in case.
推荐答案
请访问 此处的连接字符串...
了解您正在使用的 DBMS 类型也会有所帮助.我注意到您有一个 .sdf 数据库文件,而不是 DBMS(例如:MySql、SQL 或 Oracle).因此,您的连接字符串实际上取决于您想要什么.
It also would have been helpful to know what type of DBMS you are using as well. I noticed you have an .sdf database file, not a DBMS (For ex: MySql, SQL or Oracle). Therefore, your connection string is really going to depend on what you want.
顺便说一下,您的问题在这里...
Your issue is here by the way...
Private sqlConn As New SqlConnection("Data Source=(local)|DataDirectory|\Outcomes.sdf;database=Outcomes;Integrated Security=true")
*您不能使用您拥有的 SqlConnection,因为它不支持使用 .sdf 文件.
*You cant use the SqlConnection you have because its not supported with the use of .sdf files.
相反,您必须使用:System.Data.SqlServerCe '这是用于精简版
Instead you have to use: System.Data.SqlServerCe 'This is for compact edition
如果您想了解更多相关信息,请参阅 在这里.
If you would like to know more about this please see here.
这篇关于在 vb.net 中为通过 Visual Studio 2010 创建的本地数据库设置 SqlConnection 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!