SQL Server 2 ways to connect in Visual Studio _ Practical Tips

Source: Internet
Author: User
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 ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.