Use the system stored procedure sp_attach_db to attach the database;
Use the system stored procedure sp_addlogin to add an account
Add User:
1 Public 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}
Similar to the attached Database