One, SQL Server has two ways to connect to Visual Studio:
(1) Local Computer connection;
Copy Code code as follows:
string s = "Data source= computer name; initial catalog= database name; integrated security=true";
(2) Windows authentication mode connection;
Copy Code code as follows:
String cc= "Data Source = computer name; Initial Catalog = database name; User ID = sa; Password = Your password ";
second, use in Visual Studio:
Example 1: Querying the data in the database and displaying it
Copy Code code as follows:
string s = "Data source= computer name; Initial catalog= database name; integrated security=true"; Use the local Computer connection method here
SqlConnection conn = new SqlConnection (s); Create a connection
Conn. Open (); Open connection
SqlCommand cmd = conn. CreateCommand ();
Cmd.commandtext = "SELECT * from T_user"; Using commands
SqlDataAdapter Adapter=new SqlDataAdapter (CMD);
DataTable dt=new DataTable ();
Adapter. Fill (DT);
Conn. Dispose (); Release so Resources
Cmd. Dispose ();
Conn. Close (); Close connection
String Realname= "";
String Username= "";
String Mobile= "";
String Address= "";
for (int i=0;i<dt. rows.count;i++)
{
Realname=dt. ROWS[I][3]. ToString ();
Username=dt. ROWS[I][1]. ToString ();
Mobile=dt. ROWS[I][4]. ToString ();
Address=dt. ROWS[I][5]. ToString ();
Console.WriteLine ("Name is {0}, user name is {1}, mobile phone is {2}, address is {3}", Realname, username, mobile, adress);
}
Console.readkey ();
Example 2: Deleting data in a table
Copy Code code as follows:
String cc= "Data Source = computer name; Initial Catalog = database name; User ID = sa; Password = Your password "; Using Windows Authentication
SqlConnection conn = new SqlConnection (s);
Conn. Open ();
SqlCommand cmd = conn. CreateCommand ();
Cmd.commandtext = "Delete from t_user where id=5";
Cmd. ExecuteNonQuery ();
Cmd. Dispose ();
Conn. Close ();
Console.WriteLine ("Delete succeeded");
Console.readkey ();
Example 3: Modifying data in a table
Copy Code code as follows:
string s = "Data source= computer name; initial catalog= database name; integrated security=true";
SqlConnection conn = new SqlConnection (s);
Conn. Open ();
SqlCommand cmd = conn. CreateCommand ();
Cmd.commandtext = "Update t_user set card= @card where id=3";
Cmd. Parameters.addwithvalue ("@card", "13000000000000");
Cmd. ExecuteNonQuery ();
Cmd. Dispose ();
Conn. Close ();
Conn. Dispose ();
Console.WriteLine ("Modify success!") ");
Console.readkey ();
Example 4: Inserting data into a table
Copy Code code as follows:
string s = "Data source= computer name; initial catalog= database name; Integrated security= True ';
SqlConnection conn = new SqlConnection (s);
Conn. Open ();
SqlCommand cmd = conn. CreateCommand ();
cmd. CommandText = "INSERT into T_user (username,password,realname,mobile,address) VALUES (@username, @password, @realname, @ Mobile, @address) ";
cmd. Parameters.addwithvalue ("@username", "xingxing");
cmd. Parameters.addwithvalue ("@password", "77777");
cmd. Parameters.addwithvalue ("@realname", "star");
cmd. Parameters.addwithvalue ("@mobile", 1300000000);
cmd. Parameters.addwithvalue ("@address", "Beijing, Hebei Province");
cmd. ExecuteNonQuery ();
cmd. Dispose ();
Conn. Close ();
Conn. Dispose ();
Console.WriteLine ("Insert a row successfully");
Console.readkey ();