目次
データベース
サンプル4 配列内容からデータベースをコマンドで更新する
配列内容からデータベースをコマンドで更新します。
Windowsフォームプロジェクトを作成します。
フォームにボタンを貼り付けます。
フォームに接続を貼り付けてSqlConnectionを作成します。
フォームにSqlCommandを貼り付けてConnectionプロパティにSqlConnectionを指定します。
SqlCommandのCommandTextに以下のようなINSERT文を設定します。
INSERT INTO Test1 (a, b) VALUES (@Param1, @Param2)
更新ボタンを押したときにConnectionをOpenしsqlCommandのパラメータに項目を指定し、
ExecuteNonQueryメソッドで追加し、ConnectionをCloseします。
データセットを使うより実行時間は短くてすみます。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace StructTest2
{
public class Form1 : System.Windows.Forms.Form
{
class TestData { public int a ; public int b ; }
TestData[] testdata ;
private System.Windows.Forms.Button button1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlCommand sqlCommand1;
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 ;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 224);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "更新";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=\"DELL-XEON\";packet size=4096;integrated security=SSPI;data source=" +
"\"(local)\";persist security info=False;initial catalog=pubs";
//
// sqlCommand1
//
this.sqlCommand1.CommandText = "INSERT INTO Test1 (a, b) VALUES (@Param1, @Param2)";
this.sqlCommand1.Connection = this.sqlConnection1;
this.sqlCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param1", System.Data.SqlDbType.Int, 4, "a"));
this.sqlCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param2", System.Data.SqlDbType.Int, 4, "b"));
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(344, 278);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
sqlConnection1.Open() ;
for ( int i=0 ; i<4 ; i++ )
{
sqlCommand1.Parameters["@Param1"].Value = testdata[i].a ;
sqlCommand1.Parameters["@Param2"].Value = testdata[i].b ;
sqlCommand1.ExecuteNonQuery() ;
}
sqlConnection1.Close() ;
}
}
}



