string str = "user id=sa;password=000;initial catalog=glxtSQL;Server=KEYJOB;Connect Timeout=60;";
       SqlConnection conn = new SqlConnection(str);
       conn.Open();
在.cs页面写一个数据库连接类
之后在页面怎么掉用帮忙写下
   马上散分

解决方案 »

  1.   

    实例化一个类的对象,然后把con当参数传达室给查询方法
      

  2.   

    Connnection 资源很宝贵的,不建议将 Connection 当参数来传递
      

  3.   

    string str = "user id=sa;password=000;initial catalog=glxtSQL;Server=KEYJOB;Connect Timeout=60;";
           SqlConnection conn = new SqlConnection(str);
           conn.Open();
      SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from side where SideID=1";
            cmd.Connection = con;
            SqlDataReader dr=null;
             try
            {
                 dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                   Response.Write(dr["SideLeft"].ToString());
                    
                }
               
            }
            catch (Exception ex)
            {
               // return ex;
            }
      

  4.   

    直接下载sqlhelper,数据库连接执行都给你封装好了,直接调用方法就行了,何必这么麻烦?
      

  5.   

    刚开始学这个
      就是这样才可以理解学习到东西哦
          string strConnection = "user id=sa;password=123;";
                strConnection += "initial catalog=glxtSQL;Server=KEYJOB;";
                strConnection += "Connect Timeout=30";
               SqlConnection objConnection = new SqlConnection(strConnection);
                objConnection.Open();          
                string sql = "select ausername,bpassword from adminvip where ausername='" + TextBox1.Text + "' or bpassword='" + TextBox2.Text + "'";
                SqlCommand cmd = new SqlCommand(sql, objConnection);
                SqlDataReader rs = cmd.ExecuteReader();
                if (rs.Read())
                {
                    if (rs.GetValue(0).ToString().Trim() == TextBox1.Text)
                    {
                        if (rs.GetValue(1).ToString().Trim() == TextBox2.Text)
                        {   
                            Session["username"] = TextBox1.Text;
                            Response.Redirect("order.aspx");
                           
                        }
                        else
                        {
                            Label1.Text = "";
                            Label2.Text = "";
                            Label3.Text = "密码不正确!";
                        }
                    }
                    else
                    {
                        Label1.Text = "";
                        Label2.Text = "";
                        Label3.Text = "您用户名不正确!";
                    }
                }
                else
                {
                    Label1.Text = "";
                    Label2.Text = "";
                    Label3.Text = "帐号不存在!";
                }        }