The recent project boss asked for SQL Server, but the project's database was not complicated by the local demo, so decided to try VisualStudio2013 built-in SQL Server. The understanding of this thing is not much, and then the project to learn less information (perhaps I do not search the wrong way), this article as a brick, not much to say now start:
First I looked at some of the data, but a lot of it was SQL Server and not the VS built-in one, so it seemed hard to find the right material, but I found two MSDN, a simple introduction to Microsoft, on the link:
Https://msdn.microsoft.com/zh-cn/library/ms233763.aspx
Https://msdn.microsoft.com/zh-cn/library/ms171890.aspx
The first one is to teach vs built-in service-based database to build a simple. mdf file, which is a DB file (Microsoft sql Server), followed by tutorials, The process of creating does not want MySQL to be the same as the cmd Input command (I did not use the MySQL GUI), are operating on the GUI
The second is to use the WinForm to connect the first created. mdf file, and then add it can be directly displayed as a control, but the tutorial is over, I want to not directly display the data ah, I need to be in C # WPF implementation of the database additions and deletions to change ah, is to enter an ID, From the database to find a corresponding name to me, such as the operation, so continue to find methods.
Then I found this post:
http://wulin9005.blog.163.com/blog/static/13239748820133135526616/
The author, like me, is too lazy to go to SQL Server (Escape), and it teaches me to connect my Computer as server in VS, but the second step is that the server name may be different, and my workaround is:
In Vs--tools--options (bottom), Search for SQL Server
Point Database Tools--data Connections
The textbox under SQL Server Instance name is the name of your computer Server, and I didn't change it HHH
Continue with the third step to get a database called "Master",
Then right-click on the tables to add a new table, as follows:
You can be like me. You can also refer to Microsoft's first link so that the table is created
Then add the data to the table, right-click on the tables under Tb_card Show Table data, and then freely add
Okay, so we're done with the data.
The next step is to get the data in the code,
First click on Add new Data source in the View--other windows--data Sources of VS, select Database-->next in Choose a Data source type, choose a DA Select Dataset-->next in the Tabase model, click on the new Connection on the right in choose Your Data Connection, and click Data in the pop-up add Connection Change the key to the right of the source, and then select Microsoft SQL Server and click OK:
Then there will be an Add connection box as follows, ServerName mentioned above with their own, enter the bottom left corner of the test can be used to test the connection:
After success, you will return to choose your data connection, and the drop-down box will be one more option:
Select and Next,next, to the Choose your database objects, tick tables, and then finish, the rest are confirmed on the line
The server's data connections will then appear:
The connection is complete, and the end is to create WPF
Relatively humble hhh, three textbox corresponding to Cardid, username, Cardtype,
The ComboBox is used to select the database and the right Btn_refresh is used for the first time to connect and refresh the database list into the ComboBox:
#regionPress to refresh database listPrivate voidBtn_refresh_click (Objectsender, RoutedEventArgs e) {DataTable T=NewSystem.Data.DataTable (); stringCon ="server= (LocalDB) \\v11.0;database=master;integrated security=true"; Try { using(SqlConnection mycon =NewSqlConnection (Con)) {Mycon.open (); MessageBox.Show ("Open Database succeeded"); stringsql ="SELECT * FROM sys.databases"; using(SqlCommand cm =NewSqlCommand (SQL, mycon)) {SqlDataAdapter a=NewSqlDataAdapter (CM); A.fill (t); foreach(DataRow Rincht.rows) {cb_database. Items.Add (r["name"]. ToString ()); } } } } Catch(Exception ex) {MessageBox.Show ("database open failed, more information:"+Ex. ToString ()); } } #endregion
After the refresh is complete, enter 123 in the first textbox, then click Btn_open, and the other two textbox will display the corresponding data, the Btn_open code is as follows:
#regionOpen ClickPrivate voidButton_Click (Objectsender, RoutedEventArgs e) { Try { if(Cb_database. Text.trim (). Length >0) { stringCon ="server= (LocalDB) \\v11.0;database="+ Cb_database. Text.trim () +"; integrated Security=true"; using(SqlConnection mycon =NewSqlConnection (Con)) { stringsql ="Select Username,cardtype from Tb_card where cardid= '"+ tb_id. Text +"'"; using(SqlCommand cmd =NewSqlCommand (SQL, mycon)) {Mycon.open (); MessageBox.Show ("Connection Database Successful"+", ServerVersion:"+ Mycon.serverversion +";D Atasource"+Mycon.datasource); using(SqlDataReader SDR =cmd. ExecuteReader ()) {if(SDR). Read ()) {stringUserName = SDR. GetString (0). ToString (); stringCardtype = SDR. GetString (1). ToString (); Tb_name. Text=UserName; Tb_type. Text=Cardtype; } Else{MessageBox.Show ("GG"); } } } } } Else{MessageBox.Show ("Please select a database to connect to"); } } Catch(Exception ex) {MessageBox.Show ("database connection failure, details:"+Ex. ToString ()); } } #endregion
The end of the flowers ~ If God horse Advice welcome message Thank you ~
VisualStudio2013 Getting started with SQL Server built in