一: 在类中 建立数据库连接
最好使 namespace
using System.Data.SqlClient;
/// <summary>
/// Summary description for db
/// </summary>
///
namespace vote
{
public class db
{
public static SqlConnection createconnection ()
{
SqlConnection con = new SqlConnection("server=GYYKF;database=vote;uid=sa;pwd=sa;");
return con;
}
}
}
二: 绑定数据库
首先打开数据库:
SqlConnection con = Class1.createconnection();
con.Open();
然后使用命令提取数据:
如:
SqlCommand itemcmd = new SqlCommand("select votedetailsid,voteitem from votedetails where voteid=" + this.voteid, con);
SqlDataReader sdr = itemcmd.ExecuteReader();
this.rbtn.DataSource = sdr;//设置数据源
this.rbtn.DataTextField = "voteitem";
this.rbtn.DataValueField = "votedetailsid"; //绑定属性
this.rbtn.DataBind(); //把数据绑定到控件上
sdr.Close();
con.Close(); //关闭连接
三:更新数据库
首先打开数据库:
SqlConnection con = Class1.createconnection();
con.Open();
然后使用命令:
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "update votedetails set votenum=votenum+1 where id=1;
cmd.ExecuteNonQuery();//更新
con.Close();
第一次写就写这么多了,明天继续。。。。。。