一、DBAccess.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace WindowsApplication6
{
public static class DBAccess
{
private static string connectionstring = "Data source=(local)\\sqlexpress;initial catalog=pubs;integrated security=true;pooling=false;";
/// <summary>
/// 磅︽虫兵SQL粂
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExecuteSingleSql(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
try
{
SqlCommand cmd = new SqlCommand(sql, cnn);
cnn.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// sqlㄆ叭矪瞶
/// </summary>
/// <param name="list"></param>
public static void ExecuteSqlTran(ArrayList list)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
SqlTransaction tr = cnn.BeginTransaction();
cmd.Transaction = tr;
try
{
for (int n = 0; n < list.Count; n++)
{
string sql = list[n].ToString();
if (sql.Trim().Length > 0)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
tr.Commit();
}
catch (Exception ex)
{
tr.Rollback();
throw new Exception(ex.Message);
}
}
}
public static void ExecuteSqlTran2(ArrayList list)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
SqlTransaction tr = cnn.BeginTransaction();
cmd.Transaction = tr;
try
{
for (int i = 0; i < list.Count; i++)
{
string 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>
/// 浪琩琌Τ赣掸计沮
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int HasData(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
using (SqlCommand cmd = new SqlCommand(sql, cnn))
{
try
{
cnn.Open();
return (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
/// <summary>
/// DataSet摸计沮
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataSet GetDataSet(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
try
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, cnn);
cnn.Open();
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
}
二、
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ClearText()
{
this.textBox1.Text = "";
this.textBox2.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim() != "" && this.textBox2.Text.Trim() != "")
{
string sql1 = "select count(uid) from dbo.ls_t01 where uid=" + this.textBox1.Text.Trim().ToUpper() ;
if (DBAccess.HasData(sql1) == 0)
{
string sql = "insert into dbo.ls_t01 (uid,name) values (" + this.textBox1.Text.Trim().ToUpper() + ",'" + this.textBox2.Text.Trim().ToUpper() + "')";
int i = DBAccess.ExecuteSingleSql(sql);
if (i > 0)
{
this.label1.Text = "Insert ok!";
ClearText();
}
else
{
this.label1.Text = "Insert ng!";
}
button4_Click(sender, e);
}
else
{
MessageBox.Show("Uid竒");
}
}
else
{
MessageBox.Show("叫块计沮", "牡");
}
}
private void button4_Click(object sender, EventArgs e)
{
string sql = "select * from dbo.ls_t01";
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);
ClearText();
this.textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim() != "")
{
string sql = "delete dbo.ls_t01 where uid=" + this.textBox1.Text.Trim().ToUpper() + "";
int i = DBAccess.ExecuteSingleSql(sql);
if (i > 0)
{
this.label1.Text = "Delete ok!";
}
else
{
this.label1.Text = "Delete ng!";
}
}
ClearText();
button4_Click(sender, e);
}
private void button3_Click(object sender, EventArgs e)
{
ClearText();
button4_Click(sender, e);
}
}
}
三、Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsApplication6
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
四、Form1.Designer.cs
namespace WindowsApplication6
{
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = 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.button4 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Insert";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(131, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Delete";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(238, 24);
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;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 111);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(440, 296);
this.dataGridView1.TabIndex = 3;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 53);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 22);
this.textBox1.TabIndex = 4;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(138, 53);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 22);
this.textBox2.TabIndex = 5;
//
// button4
//
this.button4.Location = new System.Drawing.Point(346, 24);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 6;
this.button4.Text = "Select";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(30, 87);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 12);
this.label1.TabIndex = 7;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(464, 429);
this.Controls.Add(this.label1);
this.Controls.Add(this.button4);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
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 button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label1;
}
}