0

I'm making a login/register SQL Server system and when i made a string to connect i have this problem. My database name is Vartotojai here is the code

    public static DataTable ExecSP(string spname, List<SqlParameter> sqlParams = null)
    {
        string strConnect = "Server=DESKTOP-MR4OJCU\\MSSQLSERVER01;Database=Vartotojai;Trusted_Connection=True";

        SqlConnection conn = new SqlConnection();

        DataTable dt = new DataTable();

        try
        {
            //Connect to the DT.
            conn = new SqlConnection(strConnect);
            conn.Open();

            //Build an sql command / query 
            SqlCommand cmd = new SqlCommand(spname);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(sqlParams.ToArray());


            //Execute
            SqlCommand command = conn.CreateCommand();
            SqlDataReader dr = cmd.ExecuteReader();

            //fill dt with results
            dt.Load(dr);



        }
        catch (Exception ex)
        {

            throw ex;
        }
        finally
        {
            //ner skirtumo kas pasaidare isijungs
            conn.Close();



        }
        return dt;
    }

}

}

System.Data.SqlClient.SqlException: 'Cannot open database "Vartotojai" requested by the login. The login failed.
Login failed for user 'DESKTOP-MR4OJCU\interkodas'.'

enter image description here

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    N.B. I removed your mysql tag as it's not relevant. Please don't tag things which aren't related to your question. SQL Server and MySQL are entirely different products. – ADyson Jul 03 '18 at 13:16
  • 3
    Anyway "Login failed" means it's likely this user does not have permission to access that database on that server instance. Check your permissions, and/or set the application to log in under a different identity. Also check out https://www.connectionstrings.com/ and make sure you defined the connection string correctly for the type and version of your server. – ADyson Jul 03 '18 at 13:18
  • Try with: Data Source=DESKTOP-MR4OJCU\\MSSQLSERVER01;Initial Catalog=Vartotojai; – Prashant Pimpale Jul 03 '18 at 13:18
  • Likely user interkodas is not in Administrators group. Can you login in SSMS using windows authentication ? (It is the computer that you are on, right? If so you could use as .\\MSSQLSERVER01. – Cetin Basoz Jul 03 '18 at 13:21
  • @CetinBasoz why would you want or expect the login used by an application to be in the Adminstrators group?? That's not a good thing and it should not be necessary to just log into SQL Server – ADyson Jul 03 '18 at 13:26
  • @ADyson, It is not me who wants it. OP is asking for it using windows authentication. If not in admin group then that login should be explicitly added to MS SQL server, no? – Cetin Basoz Jul 03 '18 at 13:27
  • @CetinBasoz Sorry if I misunderstood, but the wording of your your comment strongly implied that it _ought_ to be in the Administrators group, and that its absence from there was a mistake. You're right the second time of course, ensuring the login is added explicitly in SQL Server is the correct approach, and is more than likely going to be the solution here. – ADyson Jul 03 '18 at 13:29
  • My user is on Administrator group –  Jul 03 '18 at 13:34
  • @ADyson, yes I was sort of trying to "debug" the case and point permission issue. – Cetin Basoz Jul 03 '18 at 13:34
  • OK, can you login in SSMS? Or windows authentication is not enabled? – Cetin Basoz Jul 03 '18 at 13:35
  • Yes, i can login to ssms –  Jul 03 '18 at 13:38
  • the error shows at throw ex(); –  Jul 03 '18 at 13:42
  • if you can log in via SSMS, you should be able to log in via the application as well. Check that you are definitely connecting as the same user, and to the same database instance. For clarity, your user does _not_ require to be in the Administrators group for this to work. It merely needs to be added in SQL Server as a user. – ADyson Jul 03 '18 at 13:45
  • find my answer and links, let me know if its work for you @ByByUostykLoxas – Hitesh Anshani Jul 03 '18 at 13:58
  • I forgot to say also, check that your database exists and the name is typed correctly in the connection string, and also check that your user account is enabled specifically for that database too. If you google your error message there are many previous questions about this topic and also posts on other websites. – ADyson Jul 03 '18 at 15:22

0 Answers0