首先從簡單的連接數(shù)據(jù)庫說起,本Connection寫在配置文件里,這樣寫的好處是便于代碼的維護,用戶若要更改數(shù)據(jù)庫或用戶的時候的時候程序集里的代碼可以絲毫不動,以下是代碼示例:

           

           

          <configuration>
              
          <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里參數(shù)的子參數(shù)就是在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//檢測執(zhí)行插入語句返回的行數(shù)
              SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connectionUserDB"]);
              SqlCommand cmd 
          = null; //執(zhí)行insert語句
              SqlDataAdapter da = null;//執(zhí)行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//查詢表中的行數(shù)獲取是否存在改用戶名
                      {

                          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>");
                  
                  
                  }

              
              
              }

          }


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          posts - 28, comments - 5, trackbacks - 0, articles - 0

          Copyright © 橡皮人

          主站蜘蛛池模板: 余干县| 江陵县| 沁水县| 喜德县| 鹤壁市| 金华市| 瓮安县| 文登市| 图片| 垣曲县| 叶城县| 阿鲁科尔沁旗| 专栏| 武夷山市| 汕头市| 宁南县| 林周县| 余庆县| 惠来县| 老河口市| 邢台县| 当雄县| 石河子市| 西乡县| 凤阳县| 湟源县| 象州县| 东港市| 鄂托克旗| 德庆县| 古交市| 乐昌市| 临洮县| 津市市| 承德市| 金湖县| 铜鼓县| 西充县| 报价| 宜兰县| 娱乐|