C
C#•8mo ago
Hunter1895

*.Open() works with Windows Auth, but not SQL Auth. Credentials correct.

private void loginButton_Click(object sender, RoutedEventArgs e) { //1.Address of SQL server and database
//////////string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=VisualUI_DB.mdf;User ID=[Redacted];Password=[Redacted];Connect Timeout=30"; << DOESNT WORK string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users[Redacted]\ [Redacted]\ [Redacted]\Database_VisualUI_Practice\Database_VisualUI_Practice\VisualUI_DB.mdf;Integrated Security=True;Connect Timeout=30"; // DOES WORK // 2. Establish connection using (SqlConnection conn = new SqlConnection(connString)) { // 3. Open connection conn.Open(); // 4. Prepare query string Email = emailText.Text, Pw = passwordText.Text; string query = "SELECT * FROM Users WHERE Email = '" + Email.Trim() + "' AND Password = '" + Pw.Trim() + "'"; // 5. Execute query SqlCommand cmd = new SqlCommand(query, conn); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read() == true) { UserDashboard page = new UserDashboard(); WPFUtils.SwitchToNewWindow(this, page); } else { MessageBox.Show("Your email and/or password are incorrect. Try again."); } // 6. Close Connection conn.Close(); } }
System.Data.SqlClient.SqlException: 'Cannot open database "VisualUI_DB.mdf" requested by the login. The login failed. Login failed for user '[THE LOGIN]'.'
12 Replies
Hunter1895
Hunter1895•8mo ago
I successfully log in when using the uncommented connString var, but not when using the commented connString var. Why? (Error below the code) Intention: Keep DB local to the application so it is portable to any machine. I swear my credentials are correct. In SSMS, I use this: CREATE LOGIN [REDACTED-But Im using what's here for User ID] WITH PASSWORD = '[REDACTED]'; CREATE USER LocalDBUser FOR LOGIN [THE LOGIN]; What am I doing wrong? And yes, I am new, and learning. Thank you in advance for your help. I've been at this for a cumulative of 8 hours 😦
viceroypenguin
viceroypenguin•8mo ago
You cannot use (LocalDB)\MSSQLLocalDB and use sql authentication
viceroypenguin
viceroypenguin•8mo ago
Stack Overflow
How to add username and password in sqllocaldb
I am going to start work on new Desktop application. There is I want to use light weight and standalone database so that I am going use SQL LocalDB but I want to add authentication. There I need us...
Hunter1895
Hunter1895•8mo ago
Hi vice. Im not sure I undestand. I read through it, and was able to get the pipeline and connect to it in SSMS, but how does that translate into my solution? Unless I'm misunderstanding, the post appears to be doing what you say I can't do. I also don't understand how to use the pipeline. My intention is for "example guy" to be able to download this file and use it immediately without having to download SQL on their end. Why? To practice saving/loading object data in the db. Could you expand on this a little bit?
viceroypenguin
viceroypenguin•8mo ago
Sure, so a few things: 1. There are multiple ways that you can communicate with SSMS. There's TCP/IP, Named Pipes, Shared Memory, etc. When connecting to a remote server, usually TCP/IP is the default method. 2. However, (LocalDB) uses it's own special protocol, so that it can start and stop the sql server automatically on request rather than as a Windows Service. 3. When using the default comms protocal for (LocalDB), Windows Authentication is the only authetnication protocol allowed. 4. However, you can set up an instance of LocalDB that allows SQL Authentication (username and password). This does require what is basically what's called a named instance. For this, you must start up the server from the command line, and use the named pipe to connect to it. I guess I'd come back to you with a question: are you wanting joe bloe to be able to download and be able to use it, or are you wanting someone on your company network to be able to download and use it. * in the first case, I'd use SQLite instead of MSSQL * in the second, I'd rely on MSSQL Servers on the network instead of LocalDB. * alternatively, in either case, don't use SQL authentication. Use Windows Authentication instead. I'm not sure what you're trying to address by using username/password for the db anyway.
Hunter1895
Hunter1895•8mo ago
Thank you for going deeper into it. I believe I have an understanding of those four points now. I want joe bloe to use it. Kinda like how a game has a save file locally, but I'm just practicing it with SQL at the moment. I'm very green at this. I thought the credentials were needed for what I'm trying to do. 😅 I don't remember the example now, but at some point I got a login error for Windows Auth despite having admin privileges. I'm sure I just wrote the code wrong for that. So in my case, try SQLite. Is this the correct page? https://www.sqlite.org/index.html
viceroypenguin
viceroypenguin•8mo ago
yes, that's the right db. here's documentation on using it in .net https://learn.microsoft.com/en-us/dotnet/standard/data/sqlite/?tabs=netcore-cli
Hunter1895
Hunter1895•8mo ago
I've only gotten as far as WPFs in VS and reading/writing data with MSSQL. Then I wondered how to make the DB work for anyone. Thank you, I appreciate it.
viceroypenguin
viceroypenguin•8mo ago
also take a look at EF Core SQLite https://learn.microsoft.com/en-us/ef/core/providers/sqlite/?tabs=dotnet-core-cli using efc will help you out a lot when doing db work in general.
Hunter1895
Hunter1895•8mo ago
About EntityFramework. I've read I should learn ADO.NET before using something like that. Do you have any thoughts on that? How do I know I've "learned ADO.NET?" (I am bad at attaching names to things and find I learn to do/use stuff without learning what the stuff actually is)
viceroypenguin
viceroypenguin•8mo ago
I'd say either way is fine. EFC is easier to get into, and then go back and learn ADO.NET. Or learn ADO.NET and then move to EFC to realize why working with ADO.NET sucks.
Hunter1895
Hunter1895•8mo ago
Okay! I think I have a starting point now. Thank you, Vice. I appreciate your help.