Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string loginName = "bilal";
- string password = "password";
- string serverName = "BILAL\BILAL";
- string databaseName = "MyDatabase";
- protected void CreateUser()
- {
- Server svr = new Server(serverName);
- var db = svr.Databases[databaseName];
- var IsUser = svr.Logins[loginName];
- if (db != null && IsUser == null)
- {
- Microsoft.SqlServer.Management.Smo.Login login = new Microsoft.SqlServer.Management.Smo.Login(svr, loginName);
- login.DefaultDatabase = "master";
- login.GetDatabaseUser(databaseName);
- DatabaseMapping dbMapping = new DatabaseMapping(loginName,databaseName,loginName);
- login.DefaultDatabase = databaseName;
- login.LoginType = LoginType.SqlLogin;
- login.Create(password, LoginCreateOptions.None);
- login.Enable();
- User user = new User(db, loginName);
- user.UserType = UserType.SqlLogin;
- user.Login = loginName;
- user.Create();
- // add a role
- user.AddToRole("db_owner");
- }
- CrateAndFillTable();
- }
- protected void CrateAndFillTable()
- {
- databaseName = "TestDB";
- if (conn.State == ConnectionState.Open)
- conn.Close();
- ConnectionString = "Integrated Security=true;" +
- "Initial Catalog=" + databaseName + ";" +
- "Data Source=" + serverName + ";" +
- "User Id=" + loginName + ";"+
- "Password=" + password + ";";
- conn.ConnectionString = ConnectionString;
- conn.Open();
- sql = "CREATE TABLE myTable" +
- "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +
- "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";
- cmd = new SqlCommand(sql, conn);
- try
- {
- cmd.ExecuteNonQuery();
- // Adding records into the table
- sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " +
- "VALUES (1001, 'M Bilal', 'Street1, Lahore', 23.98 ) ";
- cmd = new SqlCommand(sql, conn);
- cmd.ExecuteNonQuery();
- }
- catch (SqlException ae)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement