0

I get an error on this line of code:

sda.Fill(dtbl);

Error message:

An attempt to attach an auto-named database for file C:\Users\...\Downloads\...\hax.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

I have looked into this thread before and it did not solve my problem! Still broken.

I am a newbie to C# and this is my first SQL Server database. So I don't really know what to do. Here are some screenshots of the tables as well

Code:

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection sqlcon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\...\Downloads\...\hax.mdf;Integrated Security=True;Connect Timeout=30");

    string query = "Select * from Table Where username = '" + txtUsername.Text.Trim() + "' and password = '" + txtPassword.Text.Trim() + "'";

    SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
    DataTable dtbl = new DataTable();
    sda.Fill(dtbl);

    if (dtbl.Rows.Count == 1)
    {
        Form1 objFrmMain = new Form1();
        this.Hide();
        objFrmMain.Show();
    }
    else
    {
        MessageBox.Show("Check your username and password");
    }
}

private void button2_Click(object sender, EventArgs e)
{
    this.Close();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jason Kats
  • 61
  • 1
  • 1
  • 5
  • Do you *actually* use that filename? Because if so that's probably your problem. – Der Kommissar Dec 26 '16 at 18:50
  • Can you not use SQL connection string from MSSQL Server? – Ali Rasheed Dec 26 '16 at 18:50
  • +EBrown https://gyazo.com/f48a8375dd593465d8d6ad5f220095ea – Jason Kats Dec 26 '16 at 18:53
  • +Ali Rasheed What do you mean? Sorry I don't understand. – Jason Kats Dec 26 '16 at 18:53
  • Is that the **actual** code or did you obfuscate the file path for some reason? What I mean is, in your current code, that fails to run, does it actually say `\...\ ` with dots and all? – Lasse V. Karlsen Dec 26 '16 at 18:56
  • @Lasse V. Karlseen I did that myself, the correct path is in the code and I know that it is correct. – Jason Kats Dec 26 '16 at 19:00
  • OK, try `if (File.Exists(@"....."))` and verify that .NET thinks the file exists in that place, separate from the SqlConnection class. – Lasse V. Karlsen Dec 26 '16 at 19:03
  • I wrote a very simple IF statement stating if this exists then message box me this when I press Login. It apparently does not exist. – Jason Kats Dec 26 '16 at 19:14
  • Possible duplicate of [An attempt to attach an auto-named database for file ....database1.mdf failed](http://stackoverflow.com/questions/12566036/an-attempt-to-attach-an-auto-named-database-for-file-database1-mdf-failed) – AmiNadimi Dec 26 '16 at 19:14
  • You should never use the mdf file name in a sql connection string. Most of the time you will get credential errors because the database server owns the mdf file and will not allow other uses access. Always connect to the database using the sql server pc name and instance. – jdweng Dec 26 '16 at 20:26
  • @Jdweng any idea how to connect through server PC name and instance? Is it code or options?? – Jason Kats Dec 26 '16 at 20:40
  • I would open SQL Server Management Studio (SSMS) which comes with SQL Server. The login window will give the instance of the SQL Server to use in the connection string. The Database name will be found in the SQL Explorer Window. The connection string I normally use is as follows : "Server=.\SQLExpress;Database=DATABASE_NAME;Trusted_Connection=True;"; The period can be replace with the computer name and the instance (in this case SQLExpress) can be replace with instance found in SSMS login windows. DATABASE_NAME can be replaced with your database found in SSMS explorer. – jdweng Dec 26 '16 at 20:55

2 Answers2

0

Login to SQL server management studio and remove a database attached with the same name. that way, it will be able to auto attach your database with that name. NB: auto-attaching a database is considered a bad practice and will be removed from future SQL Server versions

KMarto
  • 300
  • 2
  • 23
  • I don't have an account there. I am using LocalDB, I think that means all I need is Studios. – Jason Kats Dec 26 '16 at 20:21
  • Just download and install the SQL Server management studio. You'll appreciate how much in control of your DB you will be. – KMarto Dec 26 '16 at 20:30
  • There's nothing much required to set it up. Just run it and select defaults. but you can check this out [http://www.sqlshack.com/sql-server-management-studio-step-step-installation-guide/] – KMarto Jan 02 '17 at 07:38
0

use the following code

sda.Fill["dtbl"];

Bokooo
  • 1
  • 2
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – R Balasubramanian Aug 03 '18 at 19:56