简体   繁体   中英

Database System.Data.SqlClient.SqlException

I'm trying to use database:

 SqlConnection sqlcon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\DB\LogiDB.mdf;Integrated Security=True;Connect Timeout=30");
            string query = "Select * from tbl_Login Where username = '" + textBox1.Text.Trim().ToLower() + "' and password = '" + textBox1.Text.Trim().ToLower() + "'";
            SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
            DataTable dtbl = new DataTable();
            sda.Fill(dtbl);
            if (dtbl.Rows.Count == 1)
            {
               //
            }

my files is:

dbo.Table.sql
LogiDB.mdf
LogiDB_log.ldf   
tbl_Login.sql    

not sure what I'm doing wrong but when I press to button I got this with line sda.Fill(dtbl); :

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Invalid object name 'tbl_Login'.

Here is my code work perfectly, You can try this and compare your code

SqlConnection cn = new SqlConnection("Data Source=AVREST\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
            cn.Open();
            SqlCommand cmd = new SqlCommand("select loginID,loginPassword from logintavle where loginID='" + textBox1.Text + "'and loginPassword='" + textBox2.Text + "'", cn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            //sda.SelectCommand = cmd;
            DataTable dataset = new DataTable();
            sda.Fill(dataset);
            if (dataset.Rows.Count > 0)

As the error says: Invalid object name 'tbl_Login'

This might mean:

  • tbl_Login table doesn't exist in you database
  • you are connecting to wrong database

Since you have tbl_Login.sql script, I guess it contains table definition. Therefore you would need to run script to create table in your LogiDB database.

Here there is example how to connect to local database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM