简体   繁体   English

数据库System.Data.SqlClient.SqlException

[英]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); 不知道我在做什么错,但是当我按下按钮时,我得到了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' 如错误所示: Invalid object name 'tbl_Login'

This might mean: 这可能意味着:

  • tbl_Login table doesn't exist in you database 数据库中不存在tbl_Login表
  • you are connecting to wrong database 您正在连接到错误的数据库

Since you have tbl_Login.sql script, I guess it contains table definition. 由于您具有tbl_Login.sql脚本,因此我猜它包含表定义。 Therefore you would need to run script to create table in your LogiDB database. 因此,您将需要运行脚本以在LogiDB数据库中创建表。

Here there is example how to connect to local database 这里有示例如何连接到本地数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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