假设我已经在Visual Studio的App_Data
文件夹中创建了一个名为Database1.mdf
的SQL Server数据库,其表名为Names
。使用C#连接到本地SQL Server数据库
我该如何建立连接以使用C#读取表值?
到目前为止,我已经试过这样的事情:
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";
但我得到一个错误:
database not found/error connecting to database
你连接数据库到SQL Server? –