C#链接数据库增删改查的例子
以users表为例,有三个字段,自增长的编号id,int类型;名称name,nvarchar类型,密码pwd,nvarchar类型
首先在vs2005中引入using System.Data.SqlClient;命名空间
/// <summary>
/// 增加
/// </summary>
/// <param name="name">姓名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public int Insert(string name,string pwd)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = "insert into users(name,pwd) values(@name,@pwd)";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlParameter parn = new SqlParameter("@name",name);
cmd.Parameters.Add(parn);
SqlParameter parp = new SqlParameter("@pwd", pwd);
cmd.Parameters.Add(parn);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功
conn.Close();
cmd.Dispose();
return result;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="name">姓名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public int Update(int id)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = "delete from users where id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn = new SqlParameter("@id", id);
cmd.Parameters.Add(parn);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功
conn.Close();
cmd.Dispose();
return result;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="name">姓名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public int Insert(string name, string pwd,int id)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = "update users set name=@name,pwd=@pwd where id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn = new SqlParameter("@name", name);
cmd.Parameters.Add(parn);
SqlParameter parp = new SqlParameter("@pwd", pwd);
cmd.Parameters.Add(parn);
SqlParameter pari = new SqlParameter("@id", id);
cmd.Parameters.Add(pari);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示修改成功
conn.Close();
cmd.Dispose();
return result;
}
/// <summary>
/// 查询
/// </summary>
/// <returns></returns>
public DataTable Select()
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = "select * from users";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
conn.Close();
cmd.Dispose();
return dt;
}
方法写好后,下面举一个查询的例子,在form窗体中拖一个DataGridView,然后在Load方法中
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = Select();
}
这样一运行,DataGridView中就会显示数据了