我做了一个小游戏,欲用一个SQL2000数据库来保存玩家的信息,客户端包括玩家注册的功能,现在我想实现客户端连接数据库,请问要怎么弄呢,就像QQ那个,登陆的时候访问数据库.
请大家帮我,谢谢

解决方案 »

  1.   

    写个配置文件 写个连接字符串
            public static SqlConnection MyConnection()
            {
                //返回数据库连接字符串
                return new SqlConnection("server=机器名\\SQL2000;database=数据库名;uid=sa;pwd=sa");
            }
      

  2.   

    做一个登陆窗口,进行身份验证。
    Private Sub cmdLogin_Click()
        On Error GoTo ErrHandle
        Dim Userid As String, UserPWD As String, ServerName As String, DatabaseName As String, strCon As String
        Userid = Trim(txtLogin(0).Text)
        UserPWD = Trim(txtLogin(1).Text)
        ServerName = Trim(txtLogin(2).Text)
        DatabaseName = Trim(txtLogin(3).Text)
        If Userid = "" Then
            MsgBox "登陆账号不能为空,请输入!", vbOKCancel, "友情提示"
            Exit Sub
        End If
        If ServerName = "" Then
            MsgBox "服务器不能为空,请输入!", vbOKCancel, "友情提示"
            Exit Sub
        ElseIf ServerName = "." Then
            ServerName = "(local)"
        End If
        If DatabaseName = "" Then
            MsgBox "数据库不能为空,请输入!", vbOKCancel, "友情提示"
            Exit Sub
        End If
        Set CN = CreateObject("ADODB.Connection")
        strCon = "Provider = Sqloledb; User ID = " & Userid & ";Password = " & UserPWD & "; Initial Catalog = " & DatabaseName & "; Data Source = " & ServerName
        'MsgBox strCon
        'CN.CursorLocation = adUseClient
        CN.Open strCon
        Unload Me
        frmModify.Show
        Exit Sub
    ErrHandle:
        MsgBox Err.Description, vbOKOnly, "友情提示"
        Screen.MousePointer = vbDefault
    End Sub
      

  3.   


    static string str="data source=.;user id=sa;initial catalog=rsgl";
    static SqlConnection sql=new SqlConnection(str);
    public static  string bm;         // 部门
    if(textBox1.Text==""||textBox2.Text=="")
    MessageBox.Show("用户名或密码不能为空");
    else
    {
    sql.Open();
    string check="select count(*)from user_table where user_id='"+textBox1.Text+"' and user_pwd='"+textBox2.Text+"'and user_bm='"+comboBox1.SelectedItem+"'";
    SqlCommand com=new SqlCommand(check,sql);
    Int32 count=(Int32)com.ExecuteScalar();
    sql.Close();
    if(count>0)
    {
    bm=comboBox1.SelectedItem.ToString();
    this.Hide();
    main m=new main();
    m.ShowDialog();
    this.Close();
    }
    else 
    {
    MessageBox.Show("用户名、密码或部门错误!");
    }
    }
      

  4.   

    ///   <summary>   
      ///   LoginFrm   的摘要说明。   
      ///   </summary>   
      public   class   LoginFrm   :   System.Windows.Forms.Form   
      {   
      public   static   bool   blCanLogin   =   false;     //记录能否检验是否通过   
      public   static   string   strUser   =   "";             //记录用户名   
      public   static   string   strDepartment   =   "";//记录通过者所属部门   
        
      private   DataSet   ds   =   new   DataSet();   
      private   DataTable   myTable;   
      private   DataRow   myRow;   
      private   string   sendStrSQL   =   "SELECT   *   from   用户清单";   
      private   System.Windows.Forms.Label   label1;   
      private   System.Windows.Forms.Label   label2;   
      private   System.Windows.Forms.Label   label3;   
      private   System.Windows.Forms.Button   btn_Cancel;   
      private   System.Windows.Forms.Button   btn_Login;   
      private   System.Windows.Forms.TextBox   txt_ID;   
      private   System.Windows.Forms.TextBox   txt_Name;   
      private   System.Windows.Forms.TextBox   txt_Pwd;   
      private   System.Windows.Forms.GroupBox   groupBox1;   
      private   System.Windows.Forms.Label   LB_Warning;   
      ///   <summary>   
      ///   必需的设计器变量。   
      ///   </summary>   
      private   System.ComponentModel.Container   components   =   null;   
        
      public   LoginFrm()   
      {   
      //   
      //   Windows   窗体设计器支持所必需的   
      //   
      InitializeComponent();   
        
      //   
      //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码   
      //   
      //初始化窗体时,从数据库中的“用户清单”表中检索数据并保存在mytable中   
      LinkDataBase   link   =   new   LinkDataBase();   
      string   sendTableName   =   "用户清单";   
      this.ds   =   link.SelectDataBase(sendStrSQL,sendTableName);   
      this.myTable   =   ds.Tables[0];   
      }   
        
      ///   <summary>   
      ///   清理所有正在使用的资源。   
      ///   </summary>   
      protected   override   void   Dispose(   bool   disposing   )   
      {   
      if(   disposing   )   
      {   
      if(components   !=   null)   
      {   
      components.Dispose();   
      }   
      }   
      base.Dispose(   disposing   );   
      }   
        
        
        
        
        
      //-------------根据输入的用户名和密码进行登录校验------------   
      private   void   btn_Login_Click(object   sender,   System.EventArgs   e)   
      {   
      for   (int   i=0;i<myTable.Rows.Count;i++)   
      {   
      this.myRow   =   myTable.Rows[i];   
      //只有当输入的用户名和密码同时对应上数据库中记录时,才能通过校验   
      if   (myRow[0].ToString().Trim()==this.txt_ID.Text.ToString().Trim()   &&   myRow[4].ToString().Trim()==this.txt_Pwd.Text.ToString().Trim())   
      {   
      blCanLogin   =   true;   
      strUser   =   myRow[0].ToString().Trim();//保存用户名   
      strDepartment   =   myRow[1].ToString().Trim();//保存部门名称   
      this.Close();//关闭窗体   
      return;   
      }   
      }   
      MessageBox.Show(   "您输入的用户号或密码不正确!");   
      return;   
      }   
        
      //-------关闭登录窗口,不进行任何操作-----------   
      private   void   btn_Cancel_Click(object   sender,   System.EventArgs   e)   
      {   
      blCanLogin   =   false;   
      this.Close();   
      }   
                      //----当鼠标离开用户号填写框,立即从数据库中检索出对于用户名称并显示----   
      private   void   txt_ID_Leave(object   sender,   System.EventArgs   e)   
      {   
      this.txt_Name.Text   =   "";   
      for   (int   i=0;i<myTable.Rows.Count;i++)   
      {   
      this.myRow   =   myTable.Rows[i];   
      if   (myRow[0].ToString().Trim()==this.txt_ID.Text.ToString().Trim())   
      {   
      this.txt_Name.Text   =   myRow[2].ToString().Trim();   
      this.txt_Pwd.Focus();   
      }   
      }   
      }   
      }
      

  5.   


    /***
      楼主注意了!
      我做的几个项目都是做一个web service! 
       所有的 客户端,都去连接,   也不用把 所有的 数据连接写在客户端,
      可以再服务器端写在web.config
     ---个人,意见!
      要是修改的,你的项目架构就要变了!
    **/