日期:2014-05-18 浏览次数:20510 次
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; } }