陌上塵

          JVAA學習筆記
          posts - 2, comments - 1, trackbacks - 0, articles - 4

          .NET與SQL SERVER數據庫連接代碼

          Posted on 2008-06-25 22:18 陌上塵 閱讀(1160) 評論(1)  編輯  收藏 所屬分類: ASP.NET
                   string struid = TextBox1.Text;
                       
           
                      //連接數據庫
           
                      string strconn ="server = 127.0.0.1;database=xsbook;uid=sa;pwd=";
                      SqlConnection conn = new SqlConnection(strconn);         
                      conn.Open();
           
                      //查詢
           
                      string strsql= "Select * from 圖書表 where 書名=  '"+struid +"'";
                          Label1.Text = strsql;
                      SqlCommand mycmd = new SqlCommand(strsql,conn); 
                      SqlDataReader rd = mycmd.ExecuteReader();
           
                         
                      //顯示查詢結果
           
                      DataGrid1.DataSource = rd;
                      DataGrid1.DataBind();     
           
                      //關閉連接
           
                      conn.Close();
           
                          ________________________________________________________________________________________                string struid = TextBox1.Text;
           
                                  //連接數據庫
                      string strconn ="server =127.0.0.1;database=xsbook;uid=sa;pwd=";
                      SqlConnection conn = new SqlConnection(strconn);
                      conn.Open( ) ;
           
                      //查詢
                                  string strsql ="Select * from 圖書表";
                                  SqlDataAdapter mycomand =new SqlDataAdapter(strsql,conn);
                      DataSet ds = new DataSet();
                      mycomand.Fill(ds,"圖書表");
           
                      //顯示查詢結果
           
                      DataGrid1.DataSource = ds.Tables["圖書表"].DefaultView;
                      DataGrid1.DataBind();     
              
                      //關閉連接
                                  conn.Close();

          Feedback

          # re: .NET與SQL SERVER數據庫連接代碼  回復  更多評論   

          2012-04-28 10:57 by 小笑
          protected void Button1_Click(object sender, EventArgs e)
          {
          string sql = "Data Source=PC-201202251849;Initial Catalog=us;Integrated Security=True";

          SqlConnection con = new SqlConnection(sql);
          string strsql = "insert into user(id,name)values('" + TextBox1.Text +"','" + TextBox2.Text +",')";// + "'," + TextBox1.Text + ",'" + TextBox1.Text,mima,shiming
          SqlCommand cmd = new SqlCommand(strsql, con);
          con.Open();
          cmd.ExecuteNonQuery();
          con.Close();

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


          網站導航:
           
          主站蜘蛛池模板: 肃南| 甘德县| 麦盖提县| 边坝县| 南皮县| 张北县| 武强县| 井研县| 岫岩| 昆山市| 平原县| 信丰县| 水城县| 广南县| 平安县| 永德县| 青海省| 米泉市| 康乐县| 井研县| 德州市| 天台县| 青海省| 青州市| 霞浦县| 伽师县| 东至县| 呼图壁县| 贺州市| 塔河县| 景洪市| 西丰县| 双柏县| 黎城县| 河源市| 图片| 承德县| 齐齐哈尔市| 工布江达县| 阿拉善右旗| 年辖:市辖区|