GetDataTable() public method

��ȡDataTable
public GetDataTable ( string sql ) : DataTable
sql string SQL���
return System.Data.DataTable
Beispiel #1
0
        public void WriteUser(User user)
        {
            string            sqlstr;
            DataRowCollection result = null;

            if (user != null && this.isConnected() == true)
            {
                sqlstr = "SELECT * FROM login WHERE username='******'";
                try
                {
                    result = db.GetDataTable(sqlstr).Rows;
                }
                catch (Exception)
                {
                    Console.WriteLine("Error: can't get user from database");
                    this.isconnected = false;
                    throw new Exception("can't get user from database");
                }
                if (result.Count > 0)
                {
                    sqlstr = string.Format("UPDATE login set password='******',sex={1},lastlogin='******' WHERE username='******'", user.Password, (int)user.Sex, user.lastLogin, user.Name);
                    try
                    {
                        db.ExeSql(sqlstr);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Error: can't create new user in database");
                        this.isconnected = false;
                        throw new Exception("can't create new user in database");
                    }
                }
                else
                {
                    sqlstr = string.Format("INSERT INTO login(username,password,sex,lastlogin) VALUES ('{0}','{1}',{2},'{3}')", user.Name, user.Password, (int)user.Sex, user.lastLogin);
                    try
                    {
                        db.ExeSql(sqlstr);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: can't create new user in database:" + ex.Message);
                        throw new Exception("can't create new user in database");
                    }
                }
            }
        }
Beispiel #2
0
        private uint getCharID(string name)
        {
            string  sqlstr;
            DataRow result = null;

            sqlstr = "SELECT * FROM  chardata  WHERE name='" + name + "'";
            try
            {
                result = db.GetDataTable(sqlstr).Rows[0];
            }
            catch (MySqlException ex)
            {
                Logger.ShowSQL(ex, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: can't get chars from database" + ex.Message);

                throw new Exception("can't get chars from database");
            }
            return((uint)result["charID"]);
        }