自己初次學習用的,各種不規范,注釋沒寫
class AdoDemo { static string strConn = @"Data Source=server1;Initial Catalog=NameCard;Integrated Security=False;User ID=sa;Password=******;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"; static void Main(string[] args) { //InsertData();//1.1插入新數據練習 //InsertDataParameters();//1.2插入新數據練習,Parameters方法 //updataData();//更新數據 //DelData();//刪除數據 //QueryDataByDs();//使用dataadapter,dataset查詢數據 //QueryByDr();//使用dataread查詢數據 //querybyDt();//使用datatable查詢數據 Console.ReadKey(); } #region 1.1插入新數據 private static void InsertData() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); using (SqlCommand sqlcmd = connection.CreateCommand()) { string strCmd = "insert into Customer (region,Name,Department) values ('jinan','li','shandong')"; sqlcmd.CommandText = strCmd; if (sqlcmd.ExecuteNonQuery() > 0) { Console.WriteLine("插入成功"); } else { Console.WriteLine("插入失敗"); } } } } #endregion #region 1.2插入新數據,Parameters方法 private static void InsertDataParameters() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); using (SqlCommand sqlCmd = connection.CreateCommand()) { string sqlStr = "insert into Customer (region,Name,Department) values (@region,@name,@depart)"; sqlCmd.CommandText = sqlStr; sqlCmd.Parameters.Add("@region", SqlDbType.NVarChar); sqlCmd.Parameters["@region"].Value = "Sichuan"; sqlCmd.Parameters.AddWithValue("@name", "Chong"); sqlCmd.Parameters.AddWithValue("@depart", "qing"); if (sqlCmd.ExecuteNonQuery() > 0) { Console.WriteLine("插入成功"); } else { Console.WriteLine("插入失敗"); } } } } #endregion #region 2 修改數據練習 private static void updataData() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); using (SqlCommand sqlCmd = connection.CreateCommand()) { string cmdStr = "update Customer set Name='@name' where id='2845'"; sqlCmd.CommandText = cmdStr; if (sqlCmd.ExecuteNonQuery() > 0) { Console.WriteLine("插入成功"); } else { Console.WriteLine("插入失敗"); } } } } #endregion #region 3 刪除數據 練習 private static void DelData() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); using (SqlCommand sqlcmd = connection.CreateCommand()) { string cmdStr = "delete from Customer where id >='2840'"; sqlcmd.CommandText = cmdStr; if (sqlcmd.ExecuteNonQuery() > 0) { Console.WriteLine("刪除成功"); } else { Console.WriteLine("刪除失敗"); } } } } #endregion #region 4.1 使用dataadapter,dataset查詢數據 private static void QueryDataByDs() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string cmdStr = "select top 20 * from Customer"; SqlDataAdapter sqlda = new SqlDataAdapter(cmdStr, strConn); DataSet ds = new DataSet(); sqlda.Fill(ds, "customer"); foreach (DataRow dr in ds.Tables[0].Rows) { Console.WriteLine("ID={0},region={1},department={2},name={3},phone1={4}", dr[0], dr[1], dr[2], dr[3], dr[4]); } } } #endregion #region 4.2 使用datareader查詢數據 private static void QueryByDr() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); using (SqlCommand sqlCmd = connection.CreateCommand()) { string cmdStr = "select top 10 * from Customer"; sqlCmd.CommandText = cmdStr; SqlDataReader sqlDr = sqlCmd.ExecuteReader(); while (sqlDr.HasRows)//是否返回數據 { while (sqlDr.Read())//從第一行開始順序讀取數據集到最后一行 { Console.WriteLine("ID={0},region={1},department={2},name={3},phone1={4}", sqlDr[0].ToString(), sqlDr[1].ToString(), sqlDr[2].ToString(), sqlDr[3].ToString(), sqlDr[4].ToString()); } } } } } #endregion #region 4.3 使用datatable查詢數據 private static void querybyDt() { SqlConnection connection = new SqlConnection(strConn); connection.Open(); string cmdStr = "select top 10 * from Customer"; SqlDataAdapter sqlda = new SqlDataAdapter(cmdStr,connection); DataTable dt = new DataTable(); sqlda.Fill(dt); foreach (DataRow item in dt.Rows) { Console.WriteLine("{0},{1},{2}", item[0].ToString(), item[1].ToString(), item[2].ToString()); } connection.Close(); } #endregion }
