Creating an 'application user' with datareader role using SMSS (not too simple)

  • Well, I have installed SQL Server Management Studio 2012, and, I am wondering how to create a new user as when I logon with the Windows user - which is the only user I remember as I forgot the 'sys' password - the scripts run by Management studio do not seem to work as expected. I run Windows 8.1, perhaps not exactly a developer's OS.

    I would like to create a user that is explicitly used by a particular application, so that eventually I can track usage, and, performance issues between the application and the database.

    I also have a difficulty trying to store images directly to the database, I am afraid that I typed in something incorrectly, or perhaps I should have initialized a connection string explicitly rather than using a "default", using Visual Studio.

    Sorry if I bring about a lot of issues to the table any ideas?

    private void btnSave_Click(object sender, EventArgs e)

    {

    try

    {

    string connectionString = TestUI.Properties.Settings.Default.UserStoreConnectionString;

    using (SqlConnection con = new SqlConnection(connectionString))

    {

    bool userExists = false;

    con.Open();

    if (userExists)

    {

    string commandText = "UPDATE [UserStore].[dbo]. SET Name = @Name, Surname = @Surname, @Userimage WHERE UserID = @user-id;";

    SqlCommand command = new SqlCommand(commandText, con);

    command.Parameters.AddWithValue("@UserId", 0);

    MemoryStream _stream = new MemoryStream();

    pcbMember.Image.Save(_stream, System.Drawing.Imaging.ImageFormat.Jpeg);

    byte[] _pic = _stream.ToArray();

    command.Parameters.AddWithValue("@UserImage", _pic);

    if (txtName.Text != null) { command.Parameters.AddWithValue("@Name", txtName.Text); }

    if (txtSurname.Text != null) { command.Parameters.AddWithValue("@Surname", txtSurname.Text); }

    command.ExecuteNonQuery();

    command.Dispose();

    Debug.WriteLine("Existing user saved.");

    }

    else if (!userExists)

    {

    int _count = 0;

    string commandText = "INSERT INTO [UserStore].[dbo]. VALUES (Name = @Name, Surname = @Surname, UserPicture = @UserPicture, UserID = @user-id);";

    SqlCommand command = new SqlCommand(commandText, con);

    command.Parameters.AddWithValue("@UserID", _count++);

    command.Parameters.AddWithValue("@Name", txtName.Text);

    command.Parameters.AddWithValue("@Surname", txtSurname.Text);

    command.Parameters.AddWithValue("@UserPicture", pcbMember.Image);

    command.ExecuteNonQuery();

    Debug.WriteLine("New user saved.");

    con.Close();

    }

    }

    }

    catch (Exception ex)

    {

    Debug.WriteLine(ex.Message);

    Debug.WriteLine(ex.Source);

    Debug.WriteLine(ex.StackTrace);

    }

    finally

    {

    }

    }

    I have spent the last two days researching these problems, feel free to follow my hyperlinks at https://delicious.com/jon80, the latest 500 links are what I was reading in the last 48 hours, so now I feel slightly confused 😉

  • What are the errors you get when you create your login?

    Or are you trying to create a database user directly in the database and not have a login?

    Or are you trying to add an application user that is stored in a database table and all permissions are managed by the application instead of the database?

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Nevermind, I must have been hitting the wrong options, as now I have created a user, and, no errors popped up. I just have a few other coding problems to resolve.

  • Cool. At least you are past the user creation.

    Good luck.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply