create username and password for database dynamically

By ashok ·

I am using Asp.Net 2.0 application, First i have created database dynamically in my codebehind. well the db created successfully. Now i want to give username and password dynamically for my dynamically created database. Is it possible? If yes, please tell me how can we do?

Dim strQuery As String

strName = txtName.Value

strQuery = "create database db" & strName & ""


Catch ex As Exception

End Try

End Sub
'Function for create database
Function createDataBase(ByVal strQuery)


Dim blnResults As Boolean = False

Dim intRowsAffected As Integer


objCmd = New SqlCommand

objCmd.Connection = objCnn

objCmd.CommandType = CommandType.Text

objCmd.CommandText = strQuery

intRowsAffected = objCmd.ExecuteNonQuery()

If intRowsAffected > 0 Then blnResults = True

Catch ex As Exception

createDataBase = ex.Message


Exit Try



End Try


createDataBase = blnResults

Catch ex As Exception

End Try

Return Nothing

End Function

The above one is created database dynamically in code behind. I need to create username and password dynamically in code behind.

Hope yours reply.

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Yes you can

by Tony Hopkinson In reply to create username and passw ...

DEpends on whether you is' an integrated login and such.

Also the syntax has changed a bit between 2k 2k5 and 2k8

Basically you add a login
with CREATR LOGIN or sp_addLogin
Then you do do sp_AddUser for teh DB and link it to the login
You might want to add serverroles as well.

Easyiest is to set an example up manually, and the script creation of the DB, with all the gubbibns.

Once you have the SQL, a few constants and Sting.Format with your variables and you are off.


Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums