一: 在類中 建立資料庫連接
最好使 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();
第一次寫就寫這麼多了,明天繼續。。。。。。