- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- namespace ADONET
- {
- class Program
- {
- static void Main(string[] args)
- {
- //【1】windows认证模式:
- // String strCon = "Data Source=localhost;Initial Catalog=SIMS;Integrated Security=true";
- //【2】SQLSERVER认证模式:
- //String strCon = "Data Source=.;Initial Catalog=SIMS;User Id=sa;Password=";
- //【3】通过工具生成连接字符串
- //String strCon = "Data Source=穆岭-PC;Initial Catalog=SIMS;Persist Security Info=True;User ID=sa Password=";
- //【4】通过连接字符串生成器生成
- SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
- scsb.DataSource = ".";
- scsb.InitialCatalog = "SIMS";
- scsb.UserID = "sa";
- scsb.Password = "";
- String strCon = scsb.ConnectionString; //得到连接字符串
- SqlConnection con = null;
- SqlCommand com = null;
- SqlDataReader sdr = null;
- try
- {
- con = new SqlConnection(strCon);
- com = con.CreateCommand();
- com.CommandText = "SELECT cname FROM Course";
- con.Open();
- sdr = com.ExecuteReader();
- while (sdr.Read())
- {
- Console.WriteLine(sdr[0]);
- }
- }catch(Exception e){
- Console.WriteLine(e.Message);
- }finally{
- sdr.Close();
- com.Clone();
- con.Close();
- }
- Console.ReadKey();
- }
- }
- }