使用C#连接到本地SQL Server数据库 [英] Connecting to local SQL Server database using C#
本文介绍了使用C#连接到本地SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我已经在Visual Studio的 App_Data
文件夹中使用名为 Names
的表创建了一个名为 Database1.mdf
的SQL Server数据库.
Suppose I have created a SQL Server database called Database1.mdf
in the App_Data
folder in Visual Studio with a table called Names
.
如何建立连接以使用C#读取表值?
How could I establish a connection to read the table values using C#?
到目前为止,我已经尝试过这样的事情:
So far I've tried something like this:
SqlConnection conn = new SqlConnection("Server=localhost;Database=Database1;");
conn.Open();
// create a SqlCommand object for this connection
SqlCommand command = conn.CreateCommand();
command.CommandText = "Select * from Names";
但是我得到一个错误:
找不到数据库/连接数据库出错
推荐答案
在数据源
(在Visual Studio的左侧)中,右键单击数据库,然后使用向导配置数据源
.将出现一个新窗口,展开连接"字符串,您可以在其中找到连接字符串
In Data Source
(on the left of Visual Studio) right click on the database, then Configure Data Source With Wizard
. A new window will appear, expand the Connection string, you can find the connection string in there
这篇关于使用C#连接到本地SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文