目次
データベース
サンプル3 配列内容からデータセットを更新する
配列内容からデータセットを更新します。
Windowsフォームプロジェクトを作成します。
フォームにデータグリッドとボタンを貼り付けます。
フォームに接続からテーブルを貼り付けてConnectionとDataAdapterを作成します。
DataAdapterからデータセットを作成します。
(ここまでのやり方はアクセスの例ですがサンプル1
アクセスのテーブルから型指定されたデータセットを作る)を見てください。
今回はSQLサーバーで話を進めます。
データセット用にNewTest1Rowで新しい行を作成し項目をうめてAddTest1Rowで追加します。(Test1の部分はデータセットのテーブル名で変わってきます)
更新ボタンを押したときにUpdateでデータベースに更新を反映します。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace StructTest
{
public class Form1 : System.Windows.Forms.Form
{
class TestData { public int a ; public int b ; }
TestData[] testdata ;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private StructTest.DataSet1 dataSet11;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
testdata = new TestData[4] ;
testdata[0] = new TestData() ; testdata[0].a = 0 ; testdata[0].b = 2 ;
testdata[1] = new TestData() ; testdata[1].a = 1 ; testdata[1].b = 4 ;
testdata[2] = new TestData() ; testdata[2].a = 2 ; testdata[2].b = 1 ;
testdata[3] = new TestData() ; testdata[3].a = 3 ; testdata[3].b = 2 ;
for ( int i=0 ; i<4 ; i++ )
{
DataSet1.Test1Row row = dataSet11.Test1.NewTest1Row() ;
row.a = testdata[i].a ;
row.b = testdata[i].b ;
dataSet11.Test1.AddTest1Row(row) ;
}
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataSet11 = new StructTest.DataSet1();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataSet11.Test1;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(464, 408);
this.dataGrid1.TabIndex = 0;
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT a, b FROM Test1";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Test1(a, b) VALUES (@a, @b); SELECT a, b FROM Test1";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@a", System.Data.SqlDbType.Int, 4, "a"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@b", System.Data.SqlDbType.Int, 4, "b"));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=\"DELL-XEON\";packet size=4096;integrated security=SSPI;data source=" +
"\"(local)\";persist security info=False;initial catalog=pubs";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Test1", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("a", "a"),
new System.Data.Common.DataColumnMapping("b", "b")})});
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("ja-JP");
//
// button1
//
this.button1.Location = new System.Drawing.Point(408, 440);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "更新";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(504, 470);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Update(dataSet11,"Test1") ;
}
}
}