I'm currently learning ADO.NET on C#. I'm learning by a book and tutorials that I found online. I wanted to try some of the samples to get myself familiarized with the whole SQL connnection and command objects and so on. Hence, I tried this:
我目前正在学习C#上的ADO.NET。我正在通过网上找到的书和教程学习。我想尝试一些示例来熟悉整个SQL连接和命令对象等等。因此,我试过这个:
namespace ConsoleApplication
{
class SqlDemo
{
public void InitConnection ()
{
string connString = @"data source=C:\SQL Server 2000 Sample Databases; database=northwnd; integrated security=SSPI";
SqlConnection conn = null;
try
{
conn = new SqlConnection (connString);
conn.Open ();
Console.WriteLine ("DataBase connection established");
}
catch
{
Console.WriteLine ("DataBase connection not established");
}
finally
{
if (conn != null) conn.Close ();
}
Console.ReadKey (true);
}
static void Main (string[] args)
{
SqlDemo d = new SqlDemo ();
d.InitConnection ();
}
}
}
And no matter how I try, I can connect to the local database. "data source=(local)"
don't work.
无论我如何尝试,我都可以连接到本地数据库。 “数据源=(本地)”不起作用。
3 个解决方案
#1
3
A couple of things:
有几件事:
1) It looks like you may have a typo in your database name. It should probably be:
1)看起来您的数据库名称可能有拼写错误。应该是:
database=northwind
2) Your data source should be (local)
or .
OR you may have an instance installed, in which case you may need to include the instance name as well, such as .\SQLExpress
or .\SQLServer
.
2)您的数据源应该是(本地)或。或者您可能已安装实例,在这种情况下,您可能还需要包含实例名称,例如。\ SQLExpress或。\ SQLServer。
#2
0
If you wish to connect to a database file using a path:
如果您希望使用路径连接到数据库文件:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
From: http://www.connectionstrings.com/sql-server-2008
However, you may also need to "Attach" the database to Sql Server. In Management studio, right click the Databases folder and select "Attach..."
但是,您可能还需要将数据库“附加”到Sql Server。在Management studio中,右键单击Databases文件夹并选择“Attach ...”
#3
0
If you are using SQL Server 2000, then just put 'local' or simply '.' (exclude the quotes) for the data source. And you have a typo in the database name. It should be 'Northwind'
如果您使用的是SQL Server 2000,那么只需输入'local'或简单地''。 (不包括引号)数据源。而且数据库名称中有拼写错误。它应该是'Northwind'
#1
3
A couple of things:
有几件事:
1) It looks like you may have a typo in your database name. It should probably be:
1)看起来您的数据库名称可能有拼写错误。应该是:
database=northwind
2) Your data source should be (local)
or .
OR you may have an instance installed, in which case you may need to include the instance name as well, such as .\SQLExpress
or .\SQLServer
.
2)您的数据源应该是(本地)或。或者您可能已安装实例,在这种情况下,您可能还需要包含实例名称,例如。\ SQLExpress或。\ SQLServer。
#2
0
If you wish to connect to a database file using a path:
如果您希望使用路径连接到数据库文件:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
From: http://www.connectionstrings.com/sql-server-2008
However, you may also need to "Attach" the database to Sql Server. In Management studio, right click the Databases folder and select "Attach..."
但是,您可能还需要将数据库“附加”到Sql Server。在Management studio中,右键单击Databases文件夹并选择“Attach ...”
#3
0
If you are using SQL Server 2000, then just put 'local' or simply '.' (exclude the quotes) for the data source. And you have a typo in the database name. It should be 'Northwind'
如果您使用的是SQL Server 2000,那么只需输入'local'或简单地''。 (不包括引号)数据源。而且数据库名称中有拼写错误。它应该是'Northwind'