详情请见压缩包,要求数据库数据随着相应操作而改变。
请各位高手帮忙呵,很急。
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient;
namespace WindowsApplication4 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6; private System.Windows.Forms.Button button7; private System.Windows.Forms.Button button8; private System.Windows.Forms.Button button9; private System.Data.SqlClient.SqlConnection sqlConnection1; private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; private System.Data.SqlClient.SqlCommand sqlSelectCommand1; private System.Data.SqlClient.SqlCommand sqlInsertCommand1; private System.Data.DataSet dataSet1; private BindingManagerBase myBind; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); DataBinding();
// // TOD 在 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.groupBox1 = new System.Windows.Forms.GroupBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.button6 = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button(); this.button8 = new System.Windows.Forms.Button(); this.button9 = new System.Windows.Forms.Button(); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.dataSet1 = new System.Data.DataSet(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.textBox3); this.groupBox1.Controls.Add(this.textBox2); this.groupBox1.Controls.Add(this.textBox1); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(8, 8); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(280, 144); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "通讯录"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(96, 112); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(152, 21); this.textBox3.TabIndex = 6; this.textBox3.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(96, 80); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(152, 21); this.textBox2.TabIndex = 5; this.textBox2.Text = ""; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(96, 48); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(152, 21); this.textBox1.TabIndex = 4; this.textBox1.Text = ""; // // label3 // this.label3.Location = new System.Drawing.Point(8, 112); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(40, 23); this.label3.TabIndex = 2; this.label3.Text = "邮件"; // // label2 // this.label2.Location = new System.Drawing.Point(8, 80); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(48, 24); this.label2.TabIndex = 1; this.label2.Text = "电话"; // // label1 // this.label1.Location = new System.Drawing.Point(8, 48); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(48, 24); this.label1.TabIndex = 0; this.label1.Text = "姓名"; // // button1 // this.button1.Location = new System.Drawing.Point(16, 168); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(56, 23); this.button1.TabIndex = 1; this.button1.Text = "最前"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(88, 168); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(56, 23); this.button2.TabIndex = 2; this.button2.Text = "前一个"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(152, 168); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(56, 23); this.button3.TabIndex = 3; this.button3.Text = "后一个"; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.Location = new System.Drawing.Point(224, 168); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(56, 23); this.button4.TabIndex = 4; this.button4.Text = "最后"; this.button4.Click += new System.EventHandler(this.button4_Click); // // button5 // this.button5.Location = new System.Drawing.Point(16, 216); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(40, 23); this.button5.TabIndex = 5; this.button5.Text = "新建"; this.button5.Click += new System.EventHandler(this.button5_Click); // // button6 // this.button6.Location = new System.Drawing.Point(72, 216); this.button6.Name = "button6"; this.button6.Size = new System.Drawing.Size(40, 23); this.button6.TabIndex = 6; this.button6.Text = "添加"; this.button6.Click += new System.EventHandler(this.button6_Click); // // button7 // this.button7.Location = new System.Drawing.Point(128, 216); this.button7.Name = "button7"; this.button7.Size = new System.Drawing.Size(40, 23); this.button7.TabIndex = 7; this.button7.Text = "修改"; this.button7.Click += new System.EventHandler(this.button7_Click); // // button8 // this.button8.Location = new System.Drawing.Point(184, 216); this.button8.Name = "button8"; this.button8.Size = new System.Drawing.Size(40, 23); this.button8.TabIndex = 8; this.button8.Text = "删除"; this.button8.Click += new System.EventHandler(this.button8_Click); // // button9 // this.button9.Location = new System.Drawing.Point(248, 216); this.button9.Name = "button9"; this.button9.Size = new System.Drawing.Size(40, 23); this.button9.TabIndex = 9; this.button9.Text = "退出"; this.button9.Click += new System.EventHandler(this.button9_Click); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id=\"NS-121\";packet size=4096;user id=sa;data source=\"NS-121\";persist " + "security info=True;initial catalog=tongxun;password=123456789"; // // sqlDataAdapter1 // 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", "tx", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("姓名", "姓名"), new System.Data.Common.DataColumnMapping("电话", "电话"), new System.Data.Common.DataColumnMapping("邮件", "邮件")})}); // // sqlInsertCommand1 // this.sqlInsertCommand1.CommandText = "INSERT INTO tx(姓名, 电话, 邮件) VALUES (@姓名, @电话, @邮件); SELECT 姓名, 电话, 邮件 FROM tx"; this.sqlInsertCommand1.Connection = this.sqlConnection1; this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@姓名", System.Data.SqlDbType.VarChar, 10, "姓名")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@电话", System.Data.SqlDbType.VarChar, 15, "电话")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@邮件", System.Data.SqlDbType.VarChar, 20, "邮件")); // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT 姓名, 电话, 邮件 FROM tx"; this.sqlSelectCommand1.Connection = this.sqlConnection1; // // dataSet1 // this.dataSet1.DataSetName = "NewDataSet"; this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN"); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button9); this.Controls.Add(this.button8); this.Controls.Add(this.button7); this.Controls.Add(this.button6); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Form1"; this.groupBox1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); this.ResumeLayout(false);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
private void button1_Click(object sender, System.EventArgs e) { this.myBind.Position=0; }
private void button2_Click(object sender, System.EventArgs e) { if(this.myBind.Position==0) { MessageBox.Show("没有前一条记录了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information ); } else { this.myBind.Position--; } }
private void button3_Click(object sender, System.EventArgs e) { if(this.myBind.Position==this.myBind.Count-1) { MessageBox.Show("没有后一条记录了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information); } else { this.myBind.Position++; } }
private void button4_Click(object sender, System.EventArgs e) { this.myBind.Position=this.myBind.Count-1; }
private void button5_Click(object sender, System.EventArgs e) { this.textBox1.Text=""; this.textBox2.Text=""; this.textBox3.Text="";
}
private void button6_Click(object sender, System.EventArgs e) { try { if(this.textBox1.Text!="" & this.textBox2.Text!="" & this.textBox3.Text!="") { //添加记录的SQL命令 string insert="INSERT INTO tx(姓名,电话,邮件) VALUES ('"; insert+=this.textBox1.Text+"', '"; insert+=this.textBox2.Text+"', '"; insert+=this.textBox3.Text+"') ";
//初始化SqlCommand SqlCommand comm=new SqlCommand(); comm.CommandText=insert; comm.Connection=this.sqlConnection1; //打开数据库连接 this.sqlConnection1.Open(); //执行添加记录的语句 comm.ExecuteNonQuery(); //关闭记录 this.sqlConnection1.Close(); //更新数据集 this.dataSet1.Tables["tx"].Rows[this.myBind.Position].BeginEdit(); this.dataSet1.Tables["tx"].Rows[this.myBind.Position].EndEdit(); this.dataSet1.AcceptChanges();
//成功提示 MessageBox.Show("添加记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); this.sqlDataAdapter1.Fill(this.dataSet1,"tx"); } else { //错误提示 MessageBox.Show("请将字段信息输入完整","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } } catch(Exception error) { //错误提示 MessageBox.Show("添加时发生错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //如果打开了连接,则关闭它 if(this.sqlConnection1.State==ConnectionState.Open) { this.sqlConnection1.Close(); } } }
private void button7_Click(object sender, System.EventArgs e) { try { //首先判断添加的信息是束完整 if(this.textBox1.Text!="" & this.textBox2.Text!="" & this.textBox3.Text!="") { //更新记录的SQL语句 string update="UPDATE tx SET "; update += "电话='"; update+= this.textBox2.Text+"', 邮件='"; update+= this.textBox3.Text+"' WHERE 姓名='"; update+= this.textBox1.Text+"'"; //初始化SqlCommand SqlCommand comm=new SqlCommand(); comm.CommandText=update; comm.Connection=this.sqlConnection1; //打开数据库连接 this.sqlConnection1.Open(); //执行添加记录的语句 comm.ExecuteNonQuery(); //关闭记录 this.sqlConnection1.Close(); //更新数据集 this.dataSet1.Tables["tx"].Rows[this.myBind.Position].BeginEdit(); this.dataSet1.Tables["tx"].Rows[this.myBind.Position].EndEdit(); this.dataSet1.AcceptChanges();
//成功提示 MessageBox.Show("修改记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); this.sqlDataAdapter1.Fill(this.dataSet1,"tx"); } else { //错误提示 MessageBox.Show("请将字段信息输入完整","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } } catch(Exception error) { //错误提示 MessageBox.Show("修改记录时发生错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //如果打开了连接,则关闭它 if(this.sqlConnection1.State==ConnectionState.Open) { this.sqlConnection1.Close(); } }
}
private void button8_Click(object sender, System.EventArgs e) { if(MessageBox.Show("是否删除记录","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.OK) { try { string delete="DELETE From tx WHERE 姓名='"; delete+=this.textBox1.Text+"'";
//初始化SqlCommand SqlCommand comm=new SqlCommand(); comm.CommandText=delete; comm.Connection=this.sqlConnection1; //打开数据库连接 this.sqlConnection1.Open(); //执行添加记录的语句 comm.ExecuteNonQuery(); //关闭记录 this.sqlConnection1.Close(); //更新数据集 this.dataSet1.Tables["tx"].Rows[this.myBind.Position].Delete(); this.dataSet1.AcceptChanges();
//成功提示 MessageBox.Show("删除记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); this.sqlDataAdapter1.Fill(this.dataSet1,"tx"); } catch(Exception error) { //错误提示 MessageBox.Show("删除记录时发生错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //如果打开了连接,则关闭它 if(this.sqlConnection1.State==ConnectionState.Open) { this.sqlConnection1.Close(); } } } }
private void button9_Click(object sender, System.EventArgs e) { this.Close(); Application.Exit(); }
} }