我在應用程序中使用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非常不錯的他在這裏寫下來...... 有人可以幫我...
我知識的缺乏在這裏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
@ yoav.str - If你有一個需要連接到SQL Server Express的本地文件,然後是的,這很難回答,因爲我不知道你是如何設置你的數據庫的,或者你希望事情的行爲如何。 – 2010-08-31 13:25:34
謝謝 I只是想從頭開始構建一個簡單的數據庫,以提高和學習,如果你有一篇文章或我可以從中讀取所有的地方 – 2010-08-31 13:34:14