您的位置:首页 > 数据库

ado.net实现对数据库的添加、删除和查看操作

2008-12-09 20:54 555 查看
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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: