无效的对象名称'TableName'
问题描述:
我是一个sql服务器的新手,我无法解决此问题。我一直在寻找2天,我完全失去了。无效的对象名称'TableName'
这是我在加载页面时得到的错误。
无效的对象名称表名'
描述: 当前Web请求执行过程中发生了未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。
异常详细信息:System.Data.SqlClient.SqlException:无效的对象 名 '表名'。
这是我的代码
private void BindListView()
{
string ConString = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
using (SqlConnection SConnection = new SqlConnection(ConString))
{
using (SqlCommand SCommand = new SqlCommand())
{
SCommand.CommandText = "SELECT * FROM TableName";
SCommand.Connection = SConnection;
using (SqlDataAdapter SDAdapter = new SqlDataAdapter(SCommand))
{
DataTable DTable = new DataTable();
SDAdapter.Fill(DTable); //This is the line where i am getting error
NewsList.DataSource = DTable;
NewsList.DataBind();
}
}
}
}
这是我的连接字符串。
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"/>
该代码工作正常,我的电脑与localhost。但是,当网站托管它不工作。
在主机上,我发现了数据库连接字符串,我更换了上面这web.config
文件。但我得到上面的错误
<add name="ApplicationServices"
connectionString="Data Source=.\MSSQLSERVER2014;Integrated Security=True;User ID=user;Password=password;Connect Timeout=15;Encrypt=False;Packet Size=4096"
providerName="System.Data.SqlClient"/>
我认为问题是连接数据库,我不知道该怎么做。
请帮
答
本地主机连接字符串指向,你将需要或者连接或移动到服务器,这样你就可以利用该数据库文件的文件数据库。
我的建议是到该数据库文件附加到本地服务器SQLEXPRESS,然后你可以当你需要发布数据库移动到托管服务器。
+0
同样在主机上的Web.config文件中,缺少以下内容:“AttachDBFilename = | DataDirectory | \ aspnetdb.mdf” –
异常告诉你它无法在数据库中找到名为“TableName”的表。 – elloco999
'TableName'表是否存在于本地主机数据库和托管数据库中? – DVJex
您尝试在同一台服务器上连接到的数据库? – Rohit