首先從簡單的連接數據庫說起,本Connection寫在配置文件里,這樣寫的好處是便于代碼的維護,用戶若要更改數據庫或用戶的時候的時候程序集里的代碼可以絲毫不動,以下是代碼示例:
<
configuration
>
< appSettings >
< add key = " connectionString " value = " server=server_name;uid=username pwd=password;database=db_name " />
</ appSettings >
/**/ /*
在appSetting里填寫一個鍵值對,用connectionString 來映射用戶的登陸詳細資料。
*/
< appSettings >
< add key = " connectionString " value = " server=server_name;uid=username pwd=password;database=db_name " />
</ appSettings >
/**/ /*
在appSetting里填寫一個鍵值對,用connectionString 來映射用戶的登陸詳細資料。
*/
web.config配置好后,在程序集里首先要導入System.Data.SqlClient這個命名空間,具體的連接方式代碼如下:
SqlConnection conn
=
new
SqlConnection(System.Configuration.ConfigurationManager.AppSettings[
"
connectionString
"
]);
/**/ /*
SqlConnection里參數的子參數就是在web.config文件里寫的“key”
*/
/**/ /*
SqlConnection里參數的子參數就是在web.config文件里寫的“key”
*/
下面以一用戶注冊這個例子講述一下連接以外的其他操作:
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Regist : System.Web.UI.Page
{
int i = 0; //檢測執行插入語句返回的行數
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connectionUserDB"]);
SqlCommand cmd = null; //執行insert語句
SqlDataAdapter da = null;//執行select語句
DataSet ds=new DataSet();
String[] shen ={ "湖南", "云南", "四川", "廣州", "貴州", "山東", "海南" };
String[] city ={ "長沙", "昆明", "成都", "深圳", "攀枝花", "濟南", "青島" };
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
setItemValues();
}
}
protected void btn_OK_Click(object sender, EventArgs e)
{
this.textIsRight();
}
public void setItemValues() {
for (int i = 0; i <shen.Length-1; ddl_shen.Items.Add(shen[i]),i++) ;
for (int j = 0; j <city.Length-1;ddl_city.Items.Add(city[j]),j++) ;
}
public void textIsRight()
{
if (this.text_email.Text.Equals("") || this.text_name.Text.Equals("") ||
this.text_pwd1.Text.Equals("") || this.text_pwd2.Text.Equals("") ||
this.rbl_sex.SelectedValue.Equals("") || this.ddl_city.SelectedValue.Equals("") ||
this.ddl_shen.SelectedValue.Equals(""))
{
Response.Write("<script>alert('請完整填寫每一項!')</script>");
}
else {
if (!this.text_pwd1.Text.Equals(this.text_pwd2.Text))
{
Response.Write("<script>alert('密碼輸入不一致!')</script>");
}
else {
getConnectionORSelect(text_name.Text);
}
}
}
public void getConnectionORSelect(String str) {
try
{
String sql_select = "select * from userInfo where name='"+str+"'";
da = new SqlDataAdapter(sql_select, conn);
da.Fill(ds, "userInfo");
DataTable table = ds.Tables["userInfo"];
if (table.Rows.Count>=1) //查詢表中的行數獲取是否存在改用戶名
{
Response.Write("<script>alert('該用戶名已存在!')</script>");
}
else
{
insertUser(text_name.Text, text_pwd1.Text, rbl_sex.SelectedItem.Text, ddl_shen.SelectedItem.Text + ddl_city.SelectedItem.Text, text_email.Text);
}
}
catch (SqlException ex)
{
Response.Write("<script>alert('" + ex.Message + "!')</script>");
}
finally {
conn.Close();
}
}
protected void insertUser(String name, String pwd, String sex, String city, String email) {
String sql_insert = "insert userInfo values('" + name + "','" + pwd + "','" + sex + "','" + city + "','" + email + "')";
try
{
Response.Write("<script>alert('" + city + "!')</script>");
conn.Open();
cmd = new SqlCommand(sql_insert, conn);
i = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
finally {
conn.Close();
}
if (i==1)
{
Response.Write("<script>alert('注冊成功!')</script>");
}
else {
Response.Write("<script>alert('注冊失敗!')</script>");
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Regist : System.Web.UI.Page
{
int i = 0; //檢測執行插入語句返回的行數
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connectionUserDB"]);
SqlCommand cmd = null; //執行insert語句
SqlDataAdapter da = null;//執行select語句
DataSet ds=new DataSet();
String[] shen ={ "湖南", "云南", "四川", "廣州", "貴州", "山東", "海南" };
String[] city ={ "長沙", "昆明", "成都", "深圳", "攀枝花", "濟南", "青島" };
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
setItemValues();
}
}
protected void btn_OK_Click(object sender, EventArgs e)
{
this.textIsRight();
}
public void setItemValues() {
for (int i = 0; i <shen.Length-1; ddl_shen.Items.Add(shen[i]),i++) ;
for (int j = 0; j <city.Length-1;ddl_city.Items.Add(city[j]),j++) ;
}
public void textIsRight()
{
if (this.text_email.Text.Equals("") || this.text_name.Text.Equals("") ||
this.text_pwd1.Text.Equals("") || this.text_pwd2.Text.Equals("") ||
this.rbl_sex.SelectedValue.Equals("") || this.ddl_city.SelectedValue.Equals("") ||
this.ddl_shen.SelectedValue.Equals(""))
{
Response.Write("<script>alert('請完整填寫每一項!')</script>");
}
else {
if (!this.text_pwd1.Text.Equals(this.text_pwd2.Text))
{
Response.Write("<script>alert('密碼輸入不一致!')</script>");
}
else {
getConnectionORSelect(text_name.Text);
}
}
}
public void getConnectionORSelect(String str) {
try
{
String sql_select = "select * from userInfo where name='"+str+"'";
da = new SqlDataAdapter(sql_select, conn);
da.Fill(ds, "userInfo");
DataTable table = ds.Tables["userInfo"];
if (table.Rows.Count>=1) //查詢表中的行數獲取是否存在改用戶名
{
Response.Write("<script>alert('該用戶名已存在!')</script>");
}
else
{
insertUser(text_name.Text, text_pwd1.Text, rbl_sex.SelectedItem.Text, ddl_shen.SelectedItem.Text + ddl_city.SelectedItem.Text, text_email.Text);
}
}
catch (SqlException ex)
{
Response.Write("<script>alert('" + ex.Message + "!')</script>");
}
finally {
conn.Close();
}
}
protected void insertUser(String name, String pwd, String sex, String city, String email) {
String sql_insert = "insert userInfo values('" + name + "','" + pwd + "','" + sex + "','" + city + "','" + email + "')";
try
{
Response.Write("<script>alert('" + city + "!')</script>");
conn.Open();
cmd = new SqlCommand(sql_insert, conn);
i = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
finally {
conn.Close();
}
if (i==1)
{
Response.Write("<script>alert('注冊成功!')</script>");
}
else {
Response.Write("<script>alert('注冊失敗!')</script>");
}
}
}