public static void ConnDb()<br /> {<br /> string strConn = "Data Source=localhost;DataBase=mytest;User ID=sa;Password=sa";<br /> string strSql = "select * from stu";<br /> //create conntion object<br /> SqlConnection dbConn = new SqlConnection(strConn);</p><p> dbConn.Open();<br /> //create command object<br /> SqlCommand dbCmd = new SqlCommand(strSql, dbConn);<br /> //use dbComm to create dbReader;<br /> SqlDataReader dbReader = dbCmd.ExecuteReader();<br /> while (dbReader.Read())<br /> {<br /> string str = "0:" + dbReader.GetInt32(0) + " 1:" + dbReader.GetString(1) + " 2:" + dbReader.GetString(2);<br /> Console.WriteLine(str);<br /> }<br /> if (null != dbReader)<br /> {<br /> dbReader.Close();<br /> }<br /> if (null != dbCmd)<br /> {<br /> dbCmd.Clone();<br /> }<br /> if (null != dbConn)<br /> {<br /> dbConn.Close();<br /> }<br /> }