首先在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=.\SQLEXPRESSInitial Catalog=TestIntegrated 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=.\SQLEXPRESSInitial Catalog=TestIntegrated 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=.\SQLEXPRESSInitial Catalog=TestIntegrated 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=.\SQLEXPRESSInitial Catalog=TestIntegrated 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中就会显示数据了
在app.config 或者web.config的connectionstring属性里面添加数据库连接字符串,然后程序取得这个串就可与其连接了。<connectionStrings>
<add name="ConnectionString" connectionString="server=.\SQLEXPRESSuid=用户名pwd=密码database=你的数据库名字"/>
</connectionStrings>
上面的server=你的数据库实例名,.\SQLEXPRESS是我这里的一个实例,根据实际修改
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)