这是我自己做的一个连接数据库与.net实现增删改查的简单例子,希望大家能我我看看那里做的不足,还需完善,希望和大家探讨和交流
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace shujukulianjie
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int j = 0;
public static int currrntrowID;//定义静态变量获取dgv当前行的ID
/// <summary>
/// BindData() 用来数据库数据的查询
/// </summary>
#region
private void BindData()
{
//定义连接对象
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=TestProject;Integrated Security=True");
string sql = "select * from stu_Info";//定义SQL语句
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);//数据适配器
DataSet ds = new DataSet();//内存中数据库对象
conn.Open();//打开连接
adapter.Fill(ds);//填充数据
conn.Close();//关闭连接
this.dgv_stu.DataSource = ds.Tables[0];//绑定数据
}
#endregion
/// <summary>
/// 窗口事件刷新dgv中的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#region
private void Form1_Load(object sender, EventArgs e)
{
BindData();
grob_Insert.Visible = false;
grop_ShanChu.Visible = false;
//dgv_stu.Size =System.Drawing.Size.(746, 285);
}
#endregion
/// <summary>
/// 删除dgv中id与文本框中所填的数字相同的一行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#region
private void btn_Delete_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=TestProject;Integrated Security=True");
string sql = " delete from stu_Info where id="+this.txt_ID.Text;//定义SQL语句
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();//打开连接
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
{
MessageBox.Show("删除成功");
this.grop_ShanChu.Visible = false;
BindData();
}
else { MessageBox.Show("删除失败"); }
}
#endregion
/// <summary>
/// 向数据库中插入数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#region
private void btn_Insert_Click(object sender, EventArgs e)
{
j = 1;
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=TestProject;Integrated Security=True");
string sql = string.Format("Insert into stu_Info(stu_Name,stu_XueHao,stu_Sex,stu_MinZu,stu_Address) values(@stu_Name,@stu_XueHao,@stu_Sex,@stu_MinZu,@stu_Address)");//定义SQL语句
SqlParameter[] sp = { new SqlParameter("@stu_Name", this.txt_Name.Text.Trim()),
new SqlParameter("@stu_XueHao",this.txt_XueHao.Text.Trim()),
new SqlParameter("@stu_Sex",this.txt_Sex.Text.Trim()),
new SqlParameter("@stu_MinZu",this.txt_MinZu.Text.Trim()),
new SqlParameter("@stu_Address",this.txt_Address.Text.Trim())
};
if (j == 1)
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();//打开连接
cmd.Parameters.AddRange(sp);
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
{
MessageBox.Show("添加成功");
this.grob_Insert.Visible = false;
BindData();
}
else { MessageBox.Show("添加失败"); }
j = 0;
}
}
#endregion
/// <summary>
/// 修改dgv中选中的某行数据的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#region
private void btn_Update_Click(object sender, EventArgs e)
{
if (j == 2)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=TestProject;Integrated Security=True");
string sql = "update stu_Info set stu_Name=@stu_Name,stu_XueHao=@stu_XueHao,stu_Sex=@stu_Sex,stu_MinZu=@stu_MinZu,stu_Address=@stu_Address where id=@id";//定义SQL语句
SqlParameter[] sp = { new SqlParameter("@stu_Name", this.txt_Name.Text.Trim()),
new SqlParameter("@stu_XueHao",this.txt_XueHao.Text.Trim()),
new SqlParameter("@stu_Sex",this.txt_Sex.Text.Trim()),
new SqlParameter("@stu_MinZu",this.txt_MinZu.Text.Trim()),
new SqlParameter("@stu_Address",this.txt_Address.Text.Trim()),
new SqlParameter("@id",currrntrowID)
};
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();//打开连接
cmd.Parameters.AddRange(sp);
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
{
MessageBox.Show("修改成功");
this.grob_Insert.Visible = false;
BindData();
}
else { MessageBox.Show("修改失败"); }
j = 0;
}
}
#endregion
/// <summary>
/// 双击dgv中的某行,获取当前行id,连接数据库根据此id查询出数据并绑定到相应文本框,进行修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#region
private void dgv_stu_MouseDoubleClick(object sender, MouseEventArgs e)
{
j = 2;
currrntrowID=Convert.ToInt32(this.dgv_stu.CurrentRow.Cells["id"].Value);
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=TestProject;Integrated Security=True");
string sql = "select stu_Name,stu_XueHao,stu_Sex,stu_MinZu,stu_Address from stu_Info where id=" + currrntrowID;//定义SQL语句
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);//数据适配器
DataSet ds = new DataSet();//内存中数据库对象
conn.Open();//打开连接
adapter.Fill(ds);//填充数据
conn.Close();//关闭连接
this.txt_Name.Text = ds.Tables[0].Rows[0]["stu_Name"].ToString();
this.txt_XueHao.Text = ds.Tables[0].Rows[0]["stu_XueHao"].ToString();
this.txt_Sex.Text = ds.Tables[0].Rows[0]["stu_Sex"].ToString();
this.txt_MinZu.Text = ds.Tables[0].Rows[0]["stu_MinZu"].ToString();
this.txt_Address.Text = ds.Tables[0].Rows[0]["stu_Address"].ToString();
}
#endregion
private void tsb_TianJia_Click(object sender, EventArgs e)
{
this.grob_Insert.Visible = true;
}
private void tsb_XiuGai_Click(object sender, EventArgs e)
{
this.grob_Insert.Visible = true;
}
private void tsb_ShanChu_Click(object sender, EventArgs e)
{
this.grop_ShanChu.Visible = true;
}
}
} |