C#操作Sql server2005 数据库,实现增删改查功能

2024-12-27 23:34:02
推荐回答(1个)
回答1:

数据库名 hospital
表名 office
字段 officeName(vachar(30)),officeType(vachar(30)),department(vachar(30))

代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace Test
{
///


/// Form1 的摘要说明。
///

public class Form1 : System.Windows.Forms.Form
{
//声明连接
private SqlConnection conn;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button4;
///
/// 必需的设计器变量。
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

///
/// 清理所有正在使用的资源。
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button4 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "新增";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(152, 272);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(304, 272);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(120, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(464, 272);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(120, 21);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(56, 104);
this.button2.Name = "button2";
this.button2.TabIndex = 0;
this.button2.Text = "修改";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(152, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(504, 248);
this.dataGrid1.TabIndex = 1;
this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged_1);
//
// button4
//
this.button4.Location = new System.Drawing.Point(56, 152);
this.button4.Name = "button4";
this.button4.TabIndex = 0;
this.button4.Text = "删除";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(664, 318);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.button4);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

///
/// 应用程序的主入口点。
///

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btn_lj_Click(object sender, System.EventArgs e)
{
//连接串
string str="Server=(local);uid=sa;pwd=;database=hospital";
//连接
conn=new SqlConnection(str);
//sql
string sql="select officeName as 科室名称 ,officeType as 科别, "+
"department as 所属部门 from office ";
//适配器
SqlDataAdapter sqldap=new SqlDataAdapter(sql,this.conn);
//内存表
DataTable dt=new DataTable();
//填充
sqldap.Fill(dt);
//数据绑定
this.dataGrid1.DataSource=dt;
}
//刷新方法
public void myreflush()
{
string sql="select officeName as 科室名称 ,officeType as 科别, "+
"department as 所属部门 from office ";
SqlDataAdapter sqldap=new SqlDataAdapter(sql,this.conn);
DataTable dt=new DataTable();
sqldap.Fill(dt);
this.dataGrid1.DataSource=dt;
}

private void button1_Click(object sender, System.EventArgs e)
{
//新增
if(this.conn.State==ConnectionState.Closed)
{
this.conn.Open();
}
string sql="insert into office values('"+this.textBox1.Text+"','"+this.textBox2.Text+"','"+this.textBox3.Text+"')";

SqlCommand sqlcmd=new SqlCommand(sql,this.conn);
int i=sqlcmd.ExecuteNonQuery();
if(i!=0)
{
MessageBox.Show("Ok");
this.myreflush();
}
else
{
MessageBox.Show("No");
}
}

//连动
private void dataGrid1_CurrentCellChanged_1(object sender, System.EventArgs e)
{
//获取当前选择行数
int iCount=this.dataGrid1.CurrentCell.RowNumber;
//数据来源
DataTable dt=(DataTable)this.dataGrid1.DataSource;
try
{
this.textBox1.Text=Convert.ToString(dt.Rows[iCount]["科室名称"]);
this.textBox2.Text=Convert.ToString(dt.Rows[iCount]["科别"]);
this.textBox3.Text=Convert.ToString(dt.Rows[iCount]["所属部门"]);
}
catch(System.IndexOutOfRangeException)
{
MessageBox.Show("此行不存在");
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
//连接串
string str="Server=(local);uid=sa;pwd=;database=hospital";
//连接
conn=new SqlConnection(str);
//sql
string sql="select officeName as 科室名称 ,officeType as 科别, "+
"department as 所属部门 from office ";
//适配器
SqlDataAdapter sqldap=new SqlDataAdapter(sql,this.conn);
//内存表
DataTable dt=new DataTable();
//填充
sqldap.Fill(dt);
//数据绑定
this.dataGrid1.DataSource=dt;
}

private void button4_Click(object sender, System.EventArgs e)
{
//删除
DialogResult dr=MessageBox.Show("是否真的删除"+this.textBox1.Text,"消息",MessageBoxButtons.OKCancel);
if(dr==DialogResult.OK)
{
if(this.conn.State==ConnectionState.Closed)
{
this.conn.Open();
}
string sql="delete from office where officeName='"+this.textBox1.Text+"'";
SqlCommand sqlcmd=new SqlCommand(sql,this.conn);
int i=sqlcmd.ExecuteNonQuery();
if(i!=0)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("No");
}
this.myreflush();
}

}

private void button2_Click(object sender, System.EventArgs e)
{
//修改
if(this.conn.State==ConnectionState.Closed)
{
this.conn.Open();
}
string sql="update office set officeType='"+this.textBox2.Text+"',department='"+this.textBox3.Text+"'"
+"where officeName='"+this.textBox1.Text+"'";
SqlCommand sqlcmd=new SqlCommand(sql,this.conn);
int i=sqlcmd.ExecuteNonQuery();
if(i!=0)
{
MessageBox.Show("OK");
this.myreflush();
}
else
{
MessageBox.Show("No");
}
}

}
}