[ADO.NET] Sample Of Update DataSet with DataAdapter
- 2007 4/12
- カテゴリー : .NET . ADO.NET . c#
- ADO.NET . c# . DataAdapter
- 投稿者 : flied_onion
- コメントを書く
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
namespace DataSetUpdateTest { class Program { static void Main(string[] args) { string response; DataSet ds = new System.Data.DataSet(); DataRow dr; DataSet newDs; DataRow[] rowsInError; DataTable newDt; DataColumn newCol; int i; SqlConnection sc = new SqlConnection(); sc.ConnectionString = "Data Source=PD3GCOT\\SQLEXPRESS;User ID=sa;Password=sa;" + "Initial Catalog=mydb;"; SqlDataAdapter sda = new SqlDataAdapter("Select * from Table_1;",sc); SqlCommandBuilder cb = new SqlCommandBuilder(sda); sc.Open(); sda.MissingSchemaAction = MissingSchemaAction.AddWithKey; sda.Fill(ds, "Table_1"); /* ds.EnforceConstraints = true; ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema; ds.WriteXmlSchema("c:\\file.xsd"); */ ds.Tables["Table_1"].Rows[0].Delete(); DataRow delRow; if (ds.HasChanges(DataRowState.Deleted)) { newDs = ds.GetChanges(DataRowState.Deleted); if (!newDs.HasErrors) { delRow = newDs.Tables["Table_1"].Rows[0]; dr = ds.Tables["Table_1"].Rows[0]; try { // the following line generates the exception Console.WriteLine(delRow[0]); } catch (Exception e) { Console.WriteLine("01:" + e.Message); } try { Console.WriteLine(delRow[0, DataRowVersion.Original]); } catch (Exception e) { Console.WriteLine("02:" + e.Message); } try { Console.WriteLine(dr[0, DataRowVersion.Original]); } catch (Exception e) { Console.WriteLine("03:" + e.Message); } // dr.AcceptChanges(); //sda.Update(ds,"Table_1"); } else { Console.WriteLine("newDS has Error"); } } else { Console.WriteLine("newDS has no changes"); } if (sc.State == ConnectionState.Open) sc.Close(); sc = null; Console.WriteLine("Update was processed successfulle?"); Console.ReadLine(); } } } |
コメントはまだありません。