Old Iron People hello Ah, I am a rookie think Kui, today I study is the database and the front-end connection used to the string, if there is any mistake hope everyone in the comment area correct. Thank you.
My environment is visual Studio Community + Microsoft SQL Server RC1 (mainly because the new UI looks better ... Well, code completion and automatic error correction are also part of the reason for debugging and presenting the relevant interfaces and data through the C # form application.
Now the goal is to test whether the connection to the database is successful, then you can set up an empty database for testing. Create a new query in SSMS enter the following code and choose to execute it well. Because win10 weird permissions problem, I put the new database in the C-Disk practice folder.
1 --create a database;2 UseMaster;3 IF db_id('EduBase2017') is not NULL4 BEGIN5 ALTER DATABASEEduBase20176 SETSingle_user7 with ROLLBACKIMMEDIATE;8 DROP DATABASEEduBase2017;9 ENDTen CREATE DATABASEEduBase2017 One on A(NAME='datafile' -, FILENAME='C:\practice\DataFile.mdf') - LOG on the(NAME='Logfile' -, FILENAME='C:\practice\Logfile.ldf');
The next step is to create a simple form application that shows up a little bit, and the UI just paints ... That's about it.
My goal now is to use the code to connect the front and back databases, which is obviously the need to call the database, but the default namespace is not covered by this part of the function, so we need this, at the top of the file, add the following line of code to access the SQL The various types of objects required by the server are included in:
1 using System.Data.SqlClient;
Namespaces add up to the ability to use SQL functions. In the Click event of the button, we first need to declare and instantiate the SQL connection (no connection is needed.) ), i.e.:
1 New SqlConnection ();
According to international practice, C # variables are my sqlConnection1 here, the first is the need to start with lowercase letters, followed by the capital Division (the big guy who created the language, I do not know how to think). The next step is the most important connection string for this wave. In a string variable, describe the server address, database name, and integrated security (that is, whether Windows authentication is used) that is required for the connection string. If the SQL Server service is not local, you need to know the specific address, but also need to turn on the remote access function on the server, otherwise there is no way to enter the servers. My SQL Server is local and can log in directly with Windows authentication, so my connection string can be written like this:
1 sqlconnection1.connectionstring = "server= (Local);D atabase=edubase2017;integrated Security=sspi";
Integrated Security is the integrated safety option, my Windows authentication directly write SSPI is enough, if the user name and password to login, you do not need to Integrated security this part, directly written on Uid=sa; Password=password just fine.
Where SA is the SQL Server installation when the system administrator added, password is installed when the password set, I did not set when the installation, so there is no way to test.
The next step is to open the SQL connection and use a form to demonstrate the relevant data.
1Sqlconnection1.open ();//open the SQL connection;2MessageBox.Show//displayed in a message box;3("Connection Status:"+ sqlConnection1.State.ToString ()//message box message content;4+"\ nthe workstation ID:"+Sqlconnection1.workstationid5+"\ n Server address:"+Sqlconnection1.datasource6+"\ n Server version:"+sqlconnection1.serverversion7+"\ nthe database name:"+Sqlconnection1.database8+"\ n (click OK to close the SQL connection)");9Sqlconnection1.close ();//close the SQL connection;TenMessageBox.Show//displayed in a message box; One("Connection Status:"+ sqlConnection1.State.ToString ());
The demo works as follows:
(When you start running)
(After you click Connect)
(After you click OK)
Then click OK to go back to the beginning of the interface, when the SQL connection has been closed.
You crossing have any doubts or find my operation where there is a mistake, comment area welcome you OH ~
Above.
Rookie of the database actual combat-connection-string