ado.net實現對數據庫的添加、刪除和查看操作。提示:添加刪除都不是查詢所以我們可以使用SqlCommand 的ExecuteNonQuery完成。 程序源碼:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Infomation
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtFlightNum;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtCertificate;
private System.Windows.Forms.TextBox txtSeatId;
private System.Windows.Forms.TextBox txtAge;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.ComboBox cboSex;
private System.Windows.Forms.Label lblFlightNum;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label lblSex;
private System.Windows.Forms.Label lblCertificate;
private System.Windows.Forms.Label lblSeatId;
private System.Windows.Forms.Label lblAge;
/// <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.lblFlightNum = new System.Windows.Forms.Label();
this.lblName = new System.Windows.Forms.Label();
this.lblSex = new System.Windows.Forms.Label();
this.lblCertificate = new System.Windows.Forms.Label();
this.lblSeatId = new System.Windows.Forms.Label();
this.lblAge = new System.Windows.Forms.Label();
this.txtFlightNum = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.txtCertificate = new System.Windows.Forms.TextBox();
this.txtSeatId = new System.Windows.Forms.TextBox();
this.txtAge = new System.Windows.Forms.TextBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.cboSex = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// lblFlightNum
//
this.lblFlightNum.Location = new System.Drawing.Point(24, 24);
this.lblFlightNum.Name = "lblFlightNum";
this.lblFlightNum.Size = new System.Drawing.Size(48, 23);
this.lblFlightNum.TabIndex = 0;
this.lblFlightNum.Text = "航班號:";
//
// lblName
//
this.lblName.Location = new System.Drawing.Point(24, 80);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(40, 23);
this.lblName.TabIndex = 1;
this.lblName.Text = "姓名:";
//
// lblSex
//
this.lblSex.Location = new System.Drawing.Point(24, 136);
this.lblSex.Name = "lblSex";
this.lblSex.Size = new System.Drawing.Size(40, 23);
this.lblSex.TabIndex = 2;
this.lblSex.Text = "性別:";
//
// lblCertificate
//
this.lblCertificate.Location = new System.Drawing.Point(256, 24);
this.lblCertificate.Name = "lblCertificate";
this.lblCertificate.Size = new System.Drawing.Size(48, 23);
this.lblCertificate.TabIndex = 3;
this.lblCertificate.Text = "證件號:";
//
// lblSeatId
//
this.lblSeatId.Location = new System.Drawing.Point(256, 80);
this.lblSeatId.Name = "lblSeatId";
this.lblSeatId.Size = new System.Drawing.Size(48, 23);
this.lblSeatId.TabIndex = 4;
this.lblSeatId.Text = "座位號:";
//
// lblAge
//
this.lblAge.Location = new System.Drawing.Point(256, 136);
this.lblAge.Name = "lblAge";
this.lblAge.Size = new System.Drawing.Size(40, 23);
this.lblAge.TabIndex = 5;
this.lblAge.Text = "年齡:";
//
// txtFlightNum
//
this.txtFlightNum.Location = new System.Drawing.Point(88, 24);
this.txtFlightNum.Name = "txtFlightNum";
this.txtFlightNum.TabIndex = 6;
this.txtFlightNum.Text = "";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(88, 80);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(136, 21);
this.txtName.TabIndex = 7;
this.txtName.Text = "";
//
// txtCertificate
//
this.txtCertificate.Location = new System.Drawing.Point(320, 24);
this.txtCertificate.Name = "txtCertificate";
this.txtCertificate.Size = new System.Drawing.Size(168, 21);
this.txtCertificate.TabIndex = 8;
this.txtCertificate.Text = "";
//
// txtSeatId
//
this.txtSeatId.Location = new System.Drawing.Point(320, 80);
this.txtSeatId.Name = "txtSeatId";
this.txtSeatId.Size = new System.Drawing.Size(168, 21);
this.txtSeatId.TabIndex = 9;
this.txtSeatId.Text = "";
//
// txtAge
//
this.txtAge.Location = new System.Drawing.Point(320, 136);
this.txtAge.Name = "txtAge";
this.txtAge.TabIndex = 10;
this.txtAge.Text = "";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(192, 216);
this.btnAdd.Name = "btnAdd";
this.btnAdd.TabIndex = 11;
this.btnAdd.Text = "添加";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(296, 216);
this.btnDelete.Name = "btnDelete";
this.btnDelete.TabIndex = 12;
this.btnDelete.Text = "刪除";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(400, 216);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 13;
this.btnCancel.Text = "取消";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// cboSex
//
this.cboSex.Items.AddRange(new object[] {
"男",
"女"});
this.cboSex.Location = new System.Drawing.Point(88, 136);
this.cboSex.Name = "cboSex";
this.cboSex.Size = new System.Drawing.Size(121, 20);
this.cboSex.TabIndex = 14;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 273);
this.Controls.Add(this.cboSex);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.txtAge);
this.Controls.Add(this.txtSeatId);
this.Controls.Add(this.txtCertificate);
this.Controls.Add(this.txtName);
this.Controls.Add(this.txtFlightNum);
this.Controls.Add(this.lblAge);
this.Controls.Add(this.lblSeatId);
this.Controls.Add(this.lblCertificate);
this.Controls.Add(this.lblSex);
this.Controls.Add(this.lblName);
this.Controls.Add(this.lblFlightNum);
this.Name = "Form1";
this.Text = "乘客詳細信息";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//創建數據庫連接對象及命令對象
private SqlConnection conn;
private SqlCommand cmd;
private void btnAdd_Click(object sender, System.EventArgs e)
{
string sql = "insert Flight values('"+this.txtFlightNum.Text+"','"+this.txtName.Text+"','"+this.cboSex.SelectedItem.ToString()+"','"+this.txtCertificate.Text+"','"+this.txtSeatId.Text+"',"+int.Parse(this.txtAge.Text)+")";
try
{
conn.Open();//打開數據庫連接
cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();//執行插入命令
MessageBox.Show("數據添加成功");//提示執行結果
}
catch(SqlException ex)//捕獲異常
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();//關閉連接
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
//程序載入時創建數據庫連接
conn = new SqlConnection("server = .;uid = sa;pwd = ;database = pubs");
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
string sql = "delete from Flight where FlightNum ='"+this.txtFlightNum.Text+"'";
try
{
conn.Open();//打開連接
cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();//執行刪除數據操作
MessageBox.Show("數據刪除成功");//提示刪除結果
}
catch(SqlException ex)//捕獲異常
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();//關閉連接
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}
ado.net實現對數據庫的添加、刪除和查看操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.