Two ways of connecting SQLServer in Visual Studio

  • 2020-06-23 00:09:45
  • OfStack

1. There are two ways to connect Sql Server to Visual Studio:
(1) Local computer connection;

string s = "Data Source= Computer name ;initial Catalog= Database name ;integrated Security=True";  

(2) windows authentication connection;

string cc="Data Source =  Computer name ; Initial Catalog =  Database name ; User ID = sa; Password =  Your password ";  

2. Use in Visual Studio:
Example 1: Query the data in the database and display it

string s = "Data Source= Computer name ;Initial Catalog= Database name ;Integrated Security=True";  // The local computer connection is used here   
SqlConnection conn = new SqlConnection(s);   // Create a connection   
conn.Open();    // Open the connection   
SqlCommand cmd = conn.CreateCommand();  
cmd.CommandText = "select * from T_User";   // Using the command   
SqlDataAdapter adapter=new SqlDataAdapter(cmd);  
DataTable dt=new DataTable();  
adapter.Fill(dt);  
conn.Dispose();  // Release all the resources   
cmd.Dispose();  
conn.Close();  // Close the 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 for {0}, The user is called {1} , a mobile phone for {2}, Address is {3}", realname, username, mobile, address);  
}  
Console.ReadKey();  

Example 2: Delete the data in the table

string cc="Data Source =  Computer name ; Initial Catalog =  Database name ; User ID = sa; Password =  Your password ";   // use windows The 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 the success ");  
Console.ReadKey();  

Example 3: Modify the data in the table

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(" Modified successfully! ");  
Console.ReadKey();  

Example 4: Insert data into the table

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", " The stars ");  
cmd.Parameters.AddWithValue("@mobile", 1300000000);  
cmd.Parameters.AddWithValue("@address", " Beijing, Hebei Province ");  
cmd.ExecuteNonQuery();  
cmd.Dispose();  
conn.Close();  
conn.Dispose();  
Console.WriteLine(" Successful insertion 1 line ");  
Console.ReadKey(); 

Related articles: