[C#] How does VS connect to the SQL Server database

Tools:
1.Visual Studio (I use vs2013)

2.SQL Server (I am using sql server2008)

Operation:
1. Open SQL Server, and you will see the initial link interface of the database after opening. (As shown below)

insert image description here
2. Copy the "Server Name" in the above picture, and then click "Connect" to enter the database.

3. Open vs and create the project you want to use. The name of the project I wrote is called: 'RestaurantSystem'

4. Tools -> Connect to Database -> In the server name, paste the copied server name

5. Select the name of the database you want to connect to below (you can also enter it manually, I am connecting to the database I created myself: RestaurantDBMS), and confirm.
insert image description here
6. Open the "Server Explorer", you will see the following information, click "Table" to see the data table created in the database

insert image description here
Connection code:
After the above operations, the database is only added to vs. If you want to edit the database in the project, you need to write some code.

1. Open your own project, open the solution explorer, right-click the project name to add a class file (the class name is selected according to your own project)
insert image description here
2. Open the class file, and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data; of;

namespace RestaurantSystem
{ class ResM //This is written in my project, named ResM; { private string MySqlCon = //I will introduce how to fill in this part below; public DataTable ExecuteQuery(string sqlStr) //Used for query; actually It is equivalent to providing a function that can pass parameters. When the time comes, write a sql statement, store it in the string, pass it to this function, and it will be executed automatically. { SqlConnection con = new SqlConnection(@MySqlCon); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = sqlStr; DataTable dt = new DataTable (); SqlDataAdapter msda; msda = new SqlDataAdapter(cmd); msda.Fill(dt); con.Close(); return dt; } public int ExecuteUpdate(string sqlStr) //used for addition, deletion and modification;



















{
SqlConnection con = new SqlConnection(@MySqlCon);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlStr;
int iud = 0;
iud = cmd.ExecuteNonQuery();
con.Close();
return iud;
}
}

}

3. Modify MySqlCon in the code. This step is used to connect to the database, which is very important.

Select the database in the "Server Resource Management", and then find the "Connection String" in the "Properties" window, copy its content, and assign it to MySqlCon. For example, I modified it to be:

MySqlCon = "Data Source=DESKTOP-0RC5ORD\SQLEXPRESS;Initial Catalog=RestaurantDBMS;Integrated Security=True";
insert image description here
After completing these operations, you can write code in the form to modify the database.

Add, delete, modify and check:
The implementation of adding, deleting, modifying and checking is all sql statements, assign the written sql statement to a string, and then execute it. It should be noted here that the above
ExecuteUpdate() function is used for addition, deletion and modification, and the ExecuteQuery() function is used for query. Next, take my code as an example:

1. Query, do not display query results (data table name is ADMINISTRATE):

String str1 = Account.Text; //There is a text box for entering account password in the form, this step is to get its content;
String str2 = Password.Text;
String str3 = “select A#,PASS FROM ADMINISTRATE WHERE A#='” + str1 + "'AND PASS='" + str2 + "'"; //Write sql statement, readers can output this sentence, in fact, the result is a standard sql statement, so readers also need this when writing their own code Format to write a sql, assign it to a string variable, and then pass it to ExecuteQuery to execute;
ResM r1 = new ResM(); //The class I wrote to connect to the database is ResM, so use it to create objects; DataTable
d1 = new DataTable(); //Because the query will return results, it is necessary to use the DataTable object to accept the returned results.
d1 = r1.ExecuteQuery(str3); //Use ExecuteQuery() to execute sql statement;
if (d1 != null && d1.Rows.Count > 0) //The query has results
{ MessageBox.Show("Welcome to use!", "Successful login", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); Form StoMes = new Storage(); //These three lines of code are to realize the interface jump; Hide(); StoMes.Show();





}

2. Query and display the query results (to achieve this step, you need to add a dataGridView control in the form to store the results)

String str1 = textBox1.Text;
String str2 = "SELECT NAME,SEX FROM PERSON WHERE P#='" + str1 + "'";
d1 = r1.ExecuteQuery(str2);
if(d1!=null&&d1.Rows.Count> 0)
{ dataGridView1.DataSource = d1; // put the query result into dataGridView;

        }

The display format of the dataGridView control can be modified through code, and you can use Baidu yourself, so I won’t introduce it here.

3. The addition, deletion, and modification are very similar. As long as there is a database foundation, you can write it yourself. Here is just an example of adding data;

String str1 = textBox1.Text; //personnel number;
String str2 = textBox2.Text; //personnel name;
String str3 = textBox3.Text; //personnel gender;

String str4 = “INSERT INTO PERSON (P#,PNAME,SEX) VALUES('” +str1 + “‘,’” + str2 + “‘,’” + str3 + “')”;

ResM r2 = new ResM();
int d2 = r2.ExecuteUpdate(str4);//There will be a return value after execution, which is int type, and will return 0 if the execution fails; if (
d2 != 0)
{ MessageBox.Show( "Add successfully!", "Add result", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Add failed! This job number already exists!", "Add result", MessageBoxButtons.OK, MessageBoxIcon. Information); }







Guess you like

Origin blog.csdn.net/qq_41661800/article/details/105373515#comments_26661501