我在SQL Server 2008 R2中的表中添加了一些列。 然后我将一些默认值放入这些列中。 然后,我使用查询选择了这些列,该查询在输出框中生成了预期结果。 我的问题是当我使用以下代码行时
if (ds.Tables[0].Rows[0]["abc"].ToString().Trim() == "") { this.abcHyperLink.ForeColor = Color.FromArgb(100, 100, 100); } else { this.abcHyperLink.NavigateUrl = ds.Tables[0].Rows[0]["abc"].ToString(); }要查询新列,它会生成此错误消息
列'abc'不属于表Table。
当我对旧列使用相同的代码时,代码工作正常并检索正确的数据。
为什么我不能查询新列,但我可以使用旧列? 在我用代码查询之前,还有什么我需要做的新列吗? 非常感谢您的帮助
编辑:
用于检查是否添加了新列的SQL查询是
Select [abc],[xyy] from [database].[dbo].[table]EDIT2:应用程序调用
public DataSet GetDataSet() { string sql = "use [name of db] exec sp_search_by '" + this.searchString + "'"; DataLayer dataLayer = new DataLayer(); return dataLayer.GetDataSet(sql, "BCITConn"); }哪个叫
public DataSet GetDataSet(string sql, string db) { string providerName = ConfigurationManager.ConnectionStrings[db].ProviderName; DbProviderFactory factory = DbProviderFactories.GetFactory(providerName); DbConnection dbConnection = new SqlConnection(); try { dbConnection = factory.CreateConnection(); dbConnection.ConnectionString = ConfigurationManager.ConnectionStrings[db].ToString(); DbCommand dbCommand = factory.CreateCommand(); dbCommand.CommandText = sql; dbCommand.CommandTimeout = 2000; dbCommand.Connection = dbConnection; DbDataAdapter dbDataAdapter = factory.CreateDataAdapter(); dbDataAdapter.SelectCommand = dbCommand; DataSet dataSet = new DataSet(); dbDataAdapter.Fill(dataSet); dbConnection.Close(); dbConnection.Dispose(); return dataSet; } catch (DbException ex) { Console.WriteLine(ex.ToString()); } finally { if (dbConnection != null) { dbConnection.Close(); dbConnection.Dispose(); } } return null; }使用
<connectionStrings> <add name="BCITConn" connectionString="Data Source=[correct]; Initial Catalog=[name of db];User ID=[correct];Password=[correct]; Max Pool Size=1000000; pooling=true" providerName="System.Data.SqlClient"/> </connectionStrings>来自网络配置
I've added some columns to a table in SQL Server 2008 R2. I then put some default values into these columns. Then I selected these columns with a query which produced the expected results in the output box. My issue is when I use the following lines of code
if (ds.Tables[0].Rows[0]["abc"].ToString().Trim() == "") { this.abcHyperLink.ForeColor = Color.FromArgb(100, 100, 100); } else { this.abcHyperLink.NavigateUrl = ds.Tables[0].Rows[0]["abc"].ToString(); }to query the new columns, it produces this error message
Column 'abc' does not belong to table Table.
When I use the same code with old columns, the code works fine and retrieves the correct data.
Why can't I query the new columns but I can with the old columns? Is there something else I have to do the new columns before I can query it with the code? Your help is much appreciated
EDIT:
The SQL query to check if the new columns were added is
Select [abc],[xyy] from [database].[dbo].[table]EDIT2: The app calls
public DataSet GetDataSet() { string sql = "use [name of db] exec sp_search_by '" + this.searchString + "'"; DataLayer dataLayer = new DataLayer(); return dataLayer.GetDataSet(sql, "BCITConn"); }which calls
public DataSet GetDataSet(string sql, string db) { string providerName = ConfigurationManager.ConnectionStrings[db].ProviderName; DbProviderFactory factory = DbProviderFactories.GetFactory(providerName); DbConnection dbConnection = new SqlConnection(); try { dbConnection = factory.CreateConnection(); dbConnection.ConnectionString = ConfigurationManager.ConnectionStrings[db].ToString(); DbCommand dbCommand = factory.CreateCommand(); dbCommand.CommandText = sql; dbCommand.CommandTimeout = 2000; dbCommand.Connection = dbConnection; DbDataAdapter dbDataAdapter = factory.CreateDataAdapter(); dbDataAdapter.SelectCommand = dbCommand; DataSet dataSet = new DataSet(); dbDataAdapter.Fill(dataSet); dbConnection.Close(); dbConnection.Dispose(); return dataSet; } catch (DbException ex) { Console.WriteLine(ex.ToString()); } finally { if (dbConnection != null) { dbConnection.Close(); dbConnection.Dispose(); } } return null; }which uses
<connectionStrings> <add name="BCITConn" connectionString="Data Source=[correct]; Initial Catalog=[name of db];User ID=[correct];Password=[correct]; Max Pool Size=1000000; pooling=true" providerName="System.Data.SqlClient"/> </connectionStrings>from the web config
最满意答案
我唯一能想到的是你要查询的列没有被渲染,也许你没有在查询中提到你的列。
The only thing I can think of is that the column you are querying is not being rendered perhaps you aren't mentioning your column in the query.
更多推荐
发布评论