using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DBTest
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Windows.Forms.DataGrid dataGrid1;
private DBTest.DataSet1 dataSet11;
private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.Button btnRefresh;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataSet11 = new DBTest.DataSet1();
this.btnUpdate = new System.Windows.Forms.Button();
this.btnRefresh = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT ID, data FROM Test";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=UCHUKAMEN;packet size=
4096;integrated security=SSPI;data source=UC" +
"HUKAMEN;persist security info=True;initial catalog=test";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Test(data) VALUES (@data);
SELECT ID, data FROM Test WHERE (ID = @@ID" +
"ENTITY)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter(
"@data", System.Data.SqlDbType.NVarChar, 50, "data"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE Test SET data =
@data WHERE (ID = @Original_ID) AND (data = @Original_data" +
" OR @Original_data IS NULL AND data IS NULL); SELECT ID, data FROM Test WHERE (I" +
"D = @ID)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter(
"@data", System.Data.SqlDbType.NVarChar, 50, "data"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter(
"@Original_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false,
((System.Byte)(0)), ((System.Byte)(0)), "ID", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter(
"@Original_data", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, false,
((System.Byte)(0)), ((System.Byte)(0)), "data", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter(
"@ID", System.Data.SqlDbType.Int, 4, "ID"));
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM Test WHERE (ID = @Original_ID) AND
(data = @Original_data OR @Origina" +
"l_data IS NULL AND data IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter(
"@Original_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false,
((System.Byte)(0)), ((System.Byte)(0)), "ID", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter(
"@Original_data", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, false,
((System.Byte)(0)), ((System.Byte)(0)), "data", System.Data.DataRowVersion.Original, null));
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
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", "Test", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("data", "data")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
//
// dataGrid1
//
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)
((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "Test";
this.dataGrid1.DataSource = this.dataSet11;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 48);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(260, 112);
this.dataGrid1.TabIndex = 0;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("ja-JP");
//
// btnUpdate
//
this.btnUpdate.Location = new System.Drawing.Point(16, 8);
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.Size = new System.Drawing.Size(56, 24);
this.btnUpdate.TabIndex = 1;
this.btnUpdate.Text = "更新";
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
//
// btnRefresh
//
this.btnRefresh.Location = new System.Drawing.Point(88, 8);
this.btnRefresh.Name = "btnRefresh";
this.btnRefresh.Size = new System.Drawing.Size(80, 24);
this.btnRefresh.TabIndex = 2;
this.btnRefresh.Text = "再読み込み";
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(256, 158);
this.Controls.Add(this.btnRefresh);
this.Controls.Add(this.btnUpdate);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.sqlDataAdapter1.Fill(this.dataSet11);
}
private void btnUpdate_Click(object sender, System.EventArgs e)
{
try
{
this.sqlDataAdapter1.Update(this.dataSet11, "Test");
}
catch (DBConcurrencyException exc)
{
MessageBox.Show(exc.Message,
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
this.dataSet11.Clear();
this.sqlDataAdapter1.Fill(this.dataSet11);
}
}
private void btnRefresh_Click(object sender, System.EventArgs e)
{
this.dataSet11.Clear();
this.sqlDataAdapter1.Fill(this.dataSet11);
}
}
}
|