//结果集的更新 同步至数据库 public static int test_ResultUpdate() { XGConnection conn = new XGConnection(); conn.ConnectionString = conn_xg; try { conn.Open(); XGCommand cmd = new XGCommand(); cmd.Connection = conn; string queryString = "select * from tp;"; XGDataAdapter adapter = new XGDataAdapter(); adapter.SelectCommand = new XGCommand(queryString, conn); string tableName = "tp"; DataSet dataSet = new DataSet(); adapter.Fill(dataSet, tableName); foreach (DataRow dr in dataSet.Tables["tp"].Rows) { if (dr["PNAME"].ToString().Trim().Equals("测试部")) { dr.Delete(); //删除DataSet 中的行 break; } } dataSet.Tables[tableName].Rows[0][1] = 38;//更新DataSet中第一行第2列的值 dataSet.Tables[tableName].Rows[1][3] = "开发项目"; string[] dd = new String[5] { "124", "24", "dsf", "dsgrt", "2016-12-12 12:11:11" }; dataSet.Tables[tableName].Rows.Add(dd);//增加一行 考虑参数的形式 //增加一行 /* string[] dd2 = new String[5] { "124", "13", "lhgdda", "第2列的值", "2016-05-13 09:15:11" }; * dataSet.Tables[tableName].Rows.Add(dd2);//再增加一行 */ adapter.Update(dataSet, tableName); // dataSet.Dispose(); adapter.Dispose(); cmd.Dispose(); conn.Close(); return(0); } catch (Exception Err) { Console.WriteLine("Error :"); Console.WriteLine("\t{0}", Err.ToString()); conn.Close(); return(-1); } }
//一般结果集的展示 public static int test_displayResult() { XGConnection conn = new XGConnection(); conn.ConnectionString = conn_xg; DataSet ds = new DataSet(); try { conn.Open(); XGCommand cmd = new XGCommand(); cmd.Connection = conn; cmd.CommandText = "select * from ta;"; XGDataAdapter t_Adapter = new XGDataAdapter(cmd); t_Adapter.Fill(ds, "ta"); //============================= for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { string val = "人员表数据: \n"; for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { val += ds.Tables[0].Rows[i][j].ToString() + "|"; //采用adapter类获取结果集数据 } } //=========== datareader string sql_str2 = "select * from tp;"; XGCommand cmd2 = conn.CreateCommand();// 或者 new DBCommand(sql_str, conn); cmd.CommandText = sql_str2; XGDataReader reader = cmd.ExecuteReader(); XGDataRecord t_Record = new XGDataRecord(reader); Console.WriteLine("结果集列数:\t{0}", t_Record.FieldCount); while (reader.Read()) { XGDataRecord t_record = new XGDataRecord(reader); string val = "人员数据: \n"; string id = reader.GetInt32(0).ToString(); string pid = reader.GetString(1); string pname = Convert.ToString(t_record[2]); //采用record类获取结果集 string desc = reader.IsDBNull(3) ? null : reader.GetString(3); string date = reader.GetDateTime(4).ToString(); val += id + "|" + pid + "|" + pname + "|" + desc + "|" + date + "|"; Console.WriteLine(val); } reader.Dispose(); cmd2.Dispose(); cmd.Dispose(); t_Adapter.Dispose(); conn.Close(); return(0); } catch (Exception Err) { Console.WriteLine("Error :"); Console.WriteLine("\t{0}", Err.ToString()); conn.Close(); return(-1); } }