在C#代码中实现在Sqlserver2000中添加用户以及附加数据库
用系统存储过程Sp_attach_db 来附加数据库;
用系统存储过程sp_addlogin 来新增帐号
添加用户:
1public bool AddUser(string cUserName,string Password,string DB)
2{
3 System.Data.SqlClient.SqlConnection oCon=new System.Data.SqlClient.SqlConnection("data source=.;initial catalog=master;password=;persist security info=True;user id=sa");
4 try
5 {
6 oCon.Open();
7 }
8 catch
9 {
10 return false;
11 }
12 System.Data.SqlClient.SqlCommand oAddUser=new System.Data.SqlClient.SqlCommand();
13 oAddUser.CommandType=System.Data.CommandType.Text;
14 oAddUser.Connection=oCon;
15 oAddUser.CommandText="exec sp_addlogin '"+cUserName+"','"+Password+"','"+DB+"'";
16 try
17 {
18 int i= oAddUser.ExecuteNonQuery();
19 }
20 catch
21 {
22 return false;
23 }
24 if(i==0)
25 {
26 return false;
27 }
28 else
29 {
30 return true;
31 }
32
33}
2{
3 System.Data.SqlClient.SqlConnection oCon=new System.Data.SqlClient.SqlConnection("data source=.;initial catalog=master;password=;persist security info=True;user id=sa");
4 try
5 {
6 oCon.Open();
7 }
8 catch
9 {
10 return false;
11 }
12 System.Data.SqlClient.SqlCommand oAddUser=new System.Data.SqlClient.SqlCommand();
13 oAddUser.CommandType=System.Data.CommandType.Text;
14 oAddUser.Connection=oCon;
15 oAddUser.CommandText="exec sp_addlogin '"+cUserName+"','"+Password+"','"+DB+"'";
16 try
17 {
18 int i= oAddUser.ExecuteNonQuery();
19 }
20 catch
21 {
22 return false;
23 }
24 if(i==0)
25 {
26 return false;
27 }
28 else
29 {
30 return true;
31 }
32
33}
附加数据库雷同