日期:2014-05-18  浏览次数:20510 次

怎么用C#让sql中数据库的创建用户在服务器角色中sysadmin打上勾?
C# code
 public bool AddUser(string cUserName, string Password, string DB)
        {
            System.Data.SqlClient.SqlConnection oCon = new System.Data.SqlClient.SqlConnection
                ("data source=.;initial catalog=master;password=334020;persist security info=True;user id=sa");
            try
            {
                oCon.Open();
            }
            catch
            {
                return false;
            }
            System.Data.SqlClient.SqlCommand oAddUser = new System.Data.SqlClient.SqlCommand();
            oAddUser.CommandType = System.Data.CommandType.Text;
            oAddUser.Connection = oCon;
            //oAddUser.CommandText="exec sp_addlogin '"+cUserName+"','"+Password+"','"+DB+"'";
            oAddUser.CommandText = "CREATE LOGIN " + textBox1.Text + " WITH PASSWord='" + textBox2.Text + "', CHECK_POLICY=OFF ";
            try
            {
                int i = oAddUser.ExecuteNonQuery();
                if (i == 0)
                {
                    return false;
                }
                else
                {
                    MessageBox.Show("添加成功");
                    return true;
                }
            }
            catch
            {
                MessageBox.Show("添加失败,可能已经存在该用户名");
                return false;
            }
        }


该怎么改呢?

------解决方案--------------------
问题解决了,加句话就可以了