using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace 上课内容1 { class 对数据的操作 { static void Main(string[] args) { //ReadDataBySqlDataReader(); //OperateDataBySqlCommand(); //ReadDataBySqlDataAdapter(); //OperateDataSqlDataAdapter(); OperateDataSqlDataAdapter2(); } /// <summary> /// 使用 SqlDataAdapter 来实现操作数据(新增,修改,删除)。 /// </summary> private static void OperateDataSqlDataAdapter() { //第一步:构造数据库连接字符串(服务器,数据库,连接认证方式:用户名,密码)。 string connectionString = "Data Source=.;Initial Catalog=Business;User Id=sa;Password=011;"; //第二步:实例化 SqlConnection。 using (SqlConnection connection = new SqlConnection(connectionString)) { //第三步:创建 SQL 语句。并构造 SqlDataAdapter 实例。 SqlDataAdapter adapter = new SqlDataAdapter(); string queryString = "SELECT TaskID,ProjectID,ModifiedBy,AssignedTo,TaskSummary,TaskDescription,PriorityID,StatusID,Progress,IsDeleted,DateDue,DateModified,DateCreated FROM Tasks"; adapter.SelectCommand = new SqlCommand(queryString, connection); //第四步:使用 SqlDataAdapter 往 DataSet 里填充数据(Fill方法)。 DataSet dataset = new DataSet(); adapter.Fill(dataset, "hello"); //第五步:往 DataSet 里添加数据(往数据表 DataTable 里添加数据行 DataRow)。 DataRow dr = dataset.Tables["hello"].NewRow(); dr["TaskID"] = Convert.ToInt32(dataset.Tables["hello"].Compute("max(TaskID)","true"))+1; dr["ProjectID"]=1; dr["ModifiedBy"]=2; dr["AssignedTo"]=1; dr["TaskSummary"]="This is the detail summary"; dr["TaskDescription"]="This is the detail description"; dr["PriorityID"]=1; dr["StatusID"]=1; dr["Progress"]=25; dr["IsDeleted"]=0; dr["DateDue"]=DateTime.Now; dr["DateModified"]=DateTime.Now; dr["DateCreated"]=DateTime.Now; dataset.Tables["hello"].Rows.Add(dr); //第六步:构造 Insert 语句,调用 SqlDataAdapter 的 Update 方法更新数据库。 string sqlInsert = "INSERT INTO Tasks(ProjectID,ModifiedBy,AssignedTo,TaskSummary,TaskDescription,PriorityID,StatusID,Progress,IsDeleted,DateDue,DateModified,DateCreated) VALUES(1,2,1,'This is the detail summary','This is the detail description',1,1,25,0,'2013-10-22','2013-10-22','2013-10-22')";//字符类型用单引号 adapter.InsertCommand = new SqlCommand(sqlInsert, connection);//此处不是SqlCommand com=new SqlCommand adapter.Update(dataset, "hello"); //第七步:修改 DataSet 中虚表的数据。 dataset.Tables["hello"].Rows[0]["ProjectID"] = 2;//注意此时为Rows[0]["ProjectId"] //第八步:构造 Update 语句,调用 SqlDataAdapter 的 Update 方法更新数据库中的实表。 string sqlUpdate = "Update Tasks set ProjectID = 2 where TaskID = 1"; adapter.UpdateCommand = new SqlCommand(sqlUpdate,connection); adapter.Update(dataset, "hello"); //第九步:删除 DataSet 中虚表的数据。 dataset.Tables["hello"].Rows[31].Delete(); //第十步:构造 Delete 语句,调用 SqlDataAdapter 的 Update 方法更新数据库中的实表。 string sqlDelete = "Delete from Tasks where TaskID = 46"; adapter.DeleteCommand = new SqlCommand(sqlDelete, connection); adapter.Update(dataset, "hello"); } } /// <summary> /// 使用 SqlDataAdapter 来读取数据。 /// </summary> private static void ReadDataBySqlDataAdapter() { //第一步:构造数据库连接字符串(服务器,数据库,连接认证方式:用户名,密码)。 string connectionString = "Data Source=.;Initial Catalog=Business;User Id=sa;Password=
//第二步:实例化 SqlConnection。 using (SqlConnection connection = new SqlConnection(connectionString)) { //第三步:创建 SQL 语句。并构造 SqlDataAdapter 实例。 SqlDataAdapter adapter = new SqlDataAdapter(); string queryString = "SELECT TaskID,ProjectID,ModifiedBy,AssignedTo,TaskSummary,TaskDescription,PriorityID,StatusID,Progress,IsDeleted,DateDue,DateModified,DateCreated FROM Tasks"; adapter.SelectCommand = new SqlCommand(queryString, connection); //connection.Open(); //第四步:使用 SqlDataAdapter 往 DataSet 里填充数据(Fill方法)。 DataSet dataset = new DataSet(); adapter.Fill(dataset, "Tasks"); //第五步:读取数据,返回结果。 foreach (DataRow row in dataset.Tables["Tasks"].Rows) { string strTemp = string.Format("{0},{1}", row[0], row[1]); Console.WriteLine(strTemp); } Console.Read(); } } /// <summary> /// 使用 SqlDataAdapter 来实现操作数据(新增,修改,删除)方法2(节省中间的 SQL 语句). /// </summary> private static void OperateDataSqlDataAdapter2() { //第一步:构造数据库连接字符串(服务器,数据库,连接认证方式:用户名,密码)。 string connectionString = "Data Source=.;Initial Catalog=Business;User Id=sa;Password=011;";
//第二步:实例化 SqlConnection。 using (SqlConnection connection = new SqlConnection(connectionString)) { //第三步:创建 SQL 语句。并构造 SqlDataAdapter 实例。 SqlDataAdapter adapter = new SqlDataAdapter(); string queryString = "SELECT TaskID,ProjectID,ModifiedBy,AssignedTo,TaskSummary,TaskDescription,PriorityID,StatusID,Progress,IsDeleted,DateDue,DateModified,DateCreated FROM Tasks"; adapter.SelectCommand = new SqlCommand(queryString, connection); //第四步:构造 SqlCommandBuilder 实例(后台自动完成SQL语句)。 SqlCommandBuilder builder = new SqlCommandBuilder(adapter); //第五步:使用 SqlDataAdapter 往 DataSet 里填充数据(Fill方法)。 DataSet dataset = new DataSet(); adapter.Fill(dataset, "Tasks"); //第六步:添加 DataSet 虚表中的数据。 DataRow dr = dataset.Tables["Tasks"].NewRow(); dr["TaskID"] = Convert.ToInt32(dataset.Tables["Tasks"].Compute("max(TaskID)","true")) +1; dr["ProjectID"] = 1; dr["ModifiedBy"] = 2; dr["AssignedTo"] = 1; dr["TaskSummary"] = "This is the detail summary"; dr["TaskDescription"] = "This is the detail description"; dr["PriorityID"] = 1; dr["StatusID"] = 1; dr["Progress"] = 25; dr["IsDeleted"] = 0; dr["DateDue"] = DateTime.Parse("2013-10-23"); dr["DateModified"] = DateTime.Parse("2013-10-23"); dr["DateCreated"] = DateTime.Parse("2013-10-23"); dataset.Tables["Tasks"].Rows.Add(dr); //第七步:修改 DataSet 虚表中的数据。 dataset.Tables["Tasks"].Rows[0]["ProjectID"] = 1; //第八步:删除 DataSet 虚表中的数据。 dataset.Tables["Tasks"].Rows[30].Delete(); //第九步:整体更新数据库实表。 builder.GetUpdateCommand(); adapter.Update(dataset, "Tasks"); //第十步:显示 dataset 中虚表中的内容。 foreach (DataRow row in dataset.Tables["Tasks"].Rows) { string strTemp = string.Format("{0},{1}", row[0], row[1]); Console.WriteLine(strTemp); } Console.Read(); } } /// <summary> /// 使用 SqlDataReader 读数据 /// </summary> private static void ReadDataBySqlDataReader() { //第一步:构造数据库连接字符串(服务器,数据库,连接认证方式:用户名,密码)。 string connectionString = "Data Source=.;Initial Catalog=Business;User Id=sa;Password=011;";
//第二步:实例化 SqlConnection。 using (SqlConnection connection = new SqlConnection(connectionString)) { //第三步:创建 SQL 语句。并构造 SqlCommand 实例。 string queryString = "SELECT TaskID,ProjectID,ModifiedBy,AssignedTo,TaskSummary,TaskDescription,PriorityID,StatusID,Progress,IsDeleted,DateDue,DateModified,DateCreated FROM Tasks"; SqlCommand command = new SqlCommand(queryString, connection); //第四步:打开数据库。 connection.Open(); //第五步:执行命令。 SqlDataReader reader = command.ExecuteReader(); //第六步:读取数据,返回结果。 try { while (reader.Read()) { string strTemp = string.Format("{0},{1}", reader["TaskID"].ToString(), reader["AssignedTo"].ToString()); Console.WriteLine(strTemp); } Console.Read(); } finally { // Always call Close when done reading. reader.Close(); } } } /// <summary> /// 修改数据库中的数据。 /// </summary> private static void OperateDataBySqlCommand() { //第一步:构造数据库连接字符串(服务器,数据库,连接认证方式:用户名,密码)。 string connectionString = "Data Source=.;Initial Catalog=Business;User Id=sa;Password=011;";
//第二步:实例化 SqlConnection。 using (SqlConnection connection = new SqlConnection(connectionString)) { //第三步:创建 SQL 语句。并构造 SqlCommand 实例。 string sqlInsert = "INSERT INTO Tasks(ProjectID,ModifiedBy,AssignedTo,TaskSummary,TaskDescription,PriorityID,StatusID,Progress,IsDeleted,DateDue,DateModified,DateCreated) VALUES(1,2,1,'This is the detail summary','This is the detail description',1,1,25,0,'2013-10-23','2013-10-23','2013-10-23')"; SqlCommand com = new SqlCommand(sqlInsert, connection); com.CommandType = CommandType.Text;//此处不要忘记 //第四步:打开数据库。 connection.Open(); //第五步:执行命令。 int intResult = com.ExecuteNonQuery(); //第六步:读取数据,返回结果。 if (intResult >= 1) { Console.WriteLine("数据插入成功!"); Console.Read(); } else { Console.WriteLine("数据插入失败!"); Console.Read(); } } } }}011;";
ADO.NET基本数据操作(增删改查)
最新推荐文章于 2024-02-18 04:00:00 发布