2010-08-31 62 views
1

我在應用程序中使用DB我的第一個步驟,所以我的目標是從無到有的一個表構建簡單數據庫,以提高和學習使用.NET C#

我已經分貝使用開闢了新的使用DB工作在Visual Studio: 通行費 - >連接到數據庫 ,並宣佈他創建新的數據基礎

的下一步是填充表4列(全4的nchar(10))

和然後(沒有實際數據裏面)我試着這個代碼:

 try 
     { 
      // step 1: create a SqlConnection object to connect to the 
      // SQL Server my server connection string proprties database 
    // i think the bug should be here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
      SqlConnection mySqlConnection = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Users/STERN/Documents/myDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");//"server=localhost;database=Northwind;uid=sa;pwd=sa"); 
      // step 2: create a SqlCommand object 
      SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); 
      // step 3: set the CommandText property of the SqlCommand object to 
      // a SQL SELECT statement that retrieves a row from the Customers table 
      mySqlCommand.CommandText = 
      "SELECT type " + 
      "FROM Table1 " + 
      "WHERE type = ‘ALFKI’"; 
      // step 4: open the database connection using the 
      // Open() method of the SqlConnection object 
      mySqlConnection.Open(); 

/* 一些代碼.... */

catch (SqlException e) 
     { 
      Console.WriteLine("A SqlException was thrown"); 
      Console.WriteLine("Number = " + e.Number); 
      Console.WriteLine("Message = " + e.Message); 
      Console.WriteLine("StackTrace:\n" + e.StackTrace); 
     } 
     string s = Console.ReadLine(); 
    } 

在異常我得到由 mySqlConnection.Open()throwen; 這是錯誤消息我得到:

A SqlException was thrown Number = 3 Message = A network-related or instance-specific error occurred while establishi ng a connection to SQL Server. The server was not found or was not accessible. V erify that the instance name is correct and that SQL Server is configured to all ow remote connections. (provider: Named Pipes Provider, error: 40 - Could not op en a connection to SQL Server) StackTrace: at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception , Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternal ConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Bool ean encrypt, Boolean trustServerCert, Boolean integratedSecurity) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeo ut, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection ow ningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnecti on owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, St ring newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdent ity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp tions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConn ection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owning Object) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection o wningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection ownin gObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection ow ningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection ou terConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at ConsoleApplication1.Program.Main(String[] args) in C:\Users\STERN\AppData\ Local\Temporary Projects\ConsoleApplication1\Program.cs:line 29

如果有人知道關於PDF,文章,這東西會幫助我,這將b非常不錯的他在這裏寫下來...... 有人可以幫我...

回答

1

看起來你無法連接到你的本地SQL Server Express實例。

即使您指定了本地數據庫文件,該文件將在連接後附加到SQL Server Express的本地實例。然後通過SQL Server Express(不直接從文件)通過附加的數據庫完成所有數據訪問。

確保啓動並運行,並且可以使用SQL Management Studio Express連接到它。

如果SQL服務器的本地實例已啓動並響應,或者你想從你的連接字符串不同的行爲,我會建議您查看ConnectionStrings.com:

SQL Server 2008 Connection Strings

+0

我知識的缺乏在這裏playes一個PRT 我怎麼知道它的工作原理微軟的文檔?並且應該將此SqlConnection mySqlConnection = new SqlConnection(「Data Source = ./ SQLEXPRESS; AttachDbFilename = C:/ Users/STERN/Documents/myDB。mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True「); //」server = localhost; database = Northwind; uid = sa; pwd = sa「); change? – 2010-08-31 13:23:48

+0

@ yoav.str - If你有一個需要連接到SQL Server Express的本地文件,然後是的,這很難回答,因爲我不知道你是如何設置你的數據庫的,或者你希望事情的行爲如何。 – 2010-08-31 13:25:34

+0

謝謝 I只是想從頭開始構建一個簡單的數據庫,以提高和學習,如果你有一篇文章或我可以從中讀取所有的地方 – 2010-08-31 13:34:14

0

嘗試連接到數據庫與SQL Server Management Studio中的相同連接字符串。它連接嗎?如果沒有(我懷疑它會),你有錯誤的連接字符串。

編輯:

此外,請確保SQL Server和SQL代理服務正在運行。

0

我不得不說,應用程序連接到SQL服務器時遇到問題,如果SQL連接字符串是正確的,請檢查您的SQL表面區域和配置,允許管道名稱和允許遠程連接。

- 編輯 -

如何配置SQL Express的遠程連接http://support.microsoft.com/kb/914277

+0

怎麼樣?有一篇文章怎麼做? – 2010-08-31 13:25:01

+0

@ yoav.str - 請參閱http://support.microsoft.com/kb/914277上的文檔 – 2010-08-31 13:39:29