Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. string loginName = "bilal";
  2. string password = "password";
  3. string serverName = "BILAL\BILAL";
  4. string databaseName = "MyDatabase";
  5.  
  6. protected void CreateUser()
  7. {
  8. Server svr = new Server(serverName);
  9. var db = svr.Databases[databaseName];
  10. var IsUser = svr.Logins[loginName];
  11. if (db != null && IsUser == null)
  12. {
  13.  
  14. Microsoft.SqlServer.Management.Smo.Login login = new Microsoft.SqlServer.Management.Smo.Login(svr, loginName);
  15. login.DefaultDatabase = "master";
  16. login.GetDatabaseUser(databaseName);
  17. DatabaseMapping dbMapping = new DatabaseMapping(loginName,databaseName,loginName);
  18. login.DefaultDatabase = databaseName;
  19. login.LoginType = LoginType.SqlLogin;
  20. login.Create(password, LoginCreateOptions.None);
  21. login.Enable();
  22.  
  23. User user = new User(db, loginName);
  24. user.UserType = UserType.SqlLogin;
  25. user.Login = loginName;
  26. user.Create();
  27. // add a role
  28. user.AddToRole("db_owner");
  29. }
  30. CrateAndFillTable();
  31. }
  32.  
  33. protected void CrateAndFillTable()
  34. {
  35. databaseName = "TestDB";
  36. if (conn.State == ConnectionState.Open)
  37. conn.Close();
  38. ConnectionString = "Integrated Security=true;" +
  39. "Initial Catalog=" + databaseName + ";" +
  40. "Data Source=" + serverName + ";" +
  41. "User Id=" + loginName + ";"+
  42. "Password=" + password + ";";
  43. conn.ConnectionString = ConnectionString;
  44. conn.Open();
  45. sql = "CREATE TABLE myTable" +
  46. "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +
  47. "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";
  48. cmd = new SqlCommand(sql, conn);
  49. try
  50. {
  51. cmd.ExecuteNonQuery();
  52.  
  53. // Adding records into the table
  54. sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " +
  55. "VALUES (1001, 'M Bilal', 'Street1, Lahore', 23.98 ) ";
  56. cmd = new SqlCommand(sql, conn);
  57. cmd.ExecuteNonQuery();
  58. }
  59. catch (SqlException ae)
  60. {
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement