winform oracle
1、DBAccess.cs
/*
add by lishuai 20110922
Just a Winform test.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
namespace WindowsApplication7
{
public static class DBAccess
{
private static string connectionstring = "data source=wip;uid=cpt;pwd=lcmmes;";
/// <summary>
/// 磅︽虫掸SQL
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExecuteSingleSql(string sql)
{
using (OracleConnection cnn = new OracleConnection(connectionstring))
{
using (OracleCommand cmd = new OracleCommand(sql, cnn))
{
try
{
cnn.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
/// <summary>
/// ㄆ叭矪瞶(SQL)
/// </summary>
/// <param name="list"></param>
public static void ExecuteSqlTran(ArrayList list)
{
using (OracleConnection cnn = new OracleConnection(connectionstring))
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = cnn;
cnn.Open();
OracleTransaction tr = cnn.BeginTransaction();
cmd.Transaction = tr;
try
{
for (int i = 0; i < list.Count; i++)
{
string sql = "";
sql = list[i].ToString();
if (sql.Trim().Length > 1)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
tr.Commit();
}
catch (Exception ex)
{
tr.Rollback();
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// DataSetΑ计沮
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataSet GetDataSet(string sql)
{
using (OracleConnection cnn = new OracleConnection(connectionstring))
{
OracleDataAdapter da = new OracleDataAdapter(sql, cnn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
}
2、Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_insert_Click(object sender, EventArgs e)
{
string sql = "insert into ls_test(usid,pwd) values (" + this.textBox1.Text.Trim() + ",'" + this.textBox2.Text.Trim().ToUpper() + "')";
if (DBAccess.ExecuteSingleSql(sql) > 0)
{
this.label1.Text = "ok";
button4_Click(sender, e);
}
else
{
this.label1.Text = "NG";
}
}
private void button4_Click(object sender, EventArgs e)
{
string sql = "select * from ls_test";
DataSet ds = new DataSet();
ds = DBAccess.GetDataSet(sql);
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void Form1_Load(object sender, EventArgs e)
{
button4_Click(sender, e);
}
private void btn_trans_Click(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
string sql = "insert into ls_test(usid,pwd) values (" + this.textBox1.Text.Trim() + ",'" + this.textBox2.Text.Trim().ToUpper() + "') ";
list.Add(sql);
sql = "insert into ls_test(usid,pwd) values (" + this.textBox3.Text.Trim() + ",'" + this.textBox4.Text.Trim().ToUpper() + "') ";
list.Add(sql);
DBAccess.ExecuteSqlTran(list);
this.label1.Text = "掸磅︽Θ";
button4_Click(sender, e);
}
}
}
3、Form1.Designer.cs
namespace WindowsApplication7
{
partial class Form1
{
/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
/// <param name="disposing">如果應該公開 Managed 資源則為 true,否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
///
/// </summary>
private void InitializeComponent()
{
this.btn_insert = new System.Windows.Forms.Button();
this.btn_delete = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.btn_trans = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// btn_insert
//
this.btn_insert.Location = new System.Drawing.Point(40, 11);
this.btn_insert.Name = "btn_insert";
this.btn_insert.Size = new System.Drawing.Size(75, 23);
this.btn_insert.TabIndex = 0;
this.btn_insert.Text = "Insert";
this.btn_insert.UseVisualStyleBackColor = true;
this.btn_insert.Click += new System.EventHandler(this.btn_insert_Click);
//
// btn_delete
//
this.btn_delete.Location = new System.Drawing.Point(132, 11);
this.btn_delete.Name = "btn_delete";
this.btn_delete.Size = new System.Drawing.Size(75, 23);
this.btn_delete.TabIndex = 1;
this.btn_delete.Text = "delete";
this.btn_delete.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Location = new System.Drawing.Point(237, 11);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "update";
this.button3.UseVisualStyleBackColor = true;
//
// button4
//
this.button4.Location = new System.Drawing.Point(336, 11);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 3;
this.button4.Text = "select";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(16, 151);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(484, 205);
this.dataGridView1.TabIndex = 4;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(40, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 22);
this.textBox1.TabIndex = 5;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(156, 40);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 22);
this.textBox2.TabIndex = 6;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(38, 116);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(33, 12);
this.label1.TabIndex = 7;
this.label1.Text = "label1";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(40, 80);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 22);
this.textBox3.TabIndex = 8;
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(156, 80);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(100, 22);
this.textBox4.TabIndex = 9;
//
// btn_trans
//
this.btn_trans.Location = new System.Drawing.Point(336, 78);
this.btn_trans.Name = "btn_trans";
this.btn_trans.Size = new System.Drawing.Size(75, 23);
this.btn_trans.TabIndex = 10;
this.btn_trans.Text = "Trans";
this.btn_trans.UseVisualStyleBackColor = true;
this.btn_trans.Click += new System.EventHandler(this.btn_trans_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(512, 368);
this.Controls.Add(this.btn_trans);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.btn_delete);
this.Controls.Add(this.btn_insert);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btn_insert;
private System.Windows.Forms.Button btn_delete;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button btn_trans;
}
}
4、Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsApplication7
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}