填充数据表 [英] Populate DataTable
本文介绍了填充数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我无法在数据库中填充DataTable.
Hi everyone, I''m having trouble populating a DataTable with a database.
public DataTable GetData()
{
string connectionString =
System.Web.HttpContext.Current.Application["solarsearch_dbConnectionString"].ToString();
SqlConnection commandString =
new SqlConnection(connectionString);
commandString.Open();
string SQL = "select * from enquiry";
SqlCommand objCommand = new SqlCommand(SQL, commandString);
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet DS = new DataSet();
adapter.SelectCommand = objCommand;
adapter.Fill(DS);
DataTable dt = DS.Tables[0];
return dt;
}
当我执行代码时,出现错误信息,
When I execute the code I get the error by
string connectionString = System.Web.HttpContext.Current.Application["solarsearch_dbConnectionString"].ToString();
说对象引用未设置为对象的实例.
请提供任何帮助.
saying Object reference not set to an instance of an object.
Any help please.
推荐答案
该错误表示"System.Web.HttpContext.Current.Application ["solarsearch_dbConnectionString"]无效-它以无效的. a测,"solarsearch_dbConnectionString"不在您的配置中...
顺便说一句:我会使用System.Configuration.Appsettings ["tag"] ..
因为它适用于两个网站的类内
应用程序和Windows应用程序.在某个时候,
您可能需要在Web上使用业务逻辑
桌面上的应用程序.
The error means that "System.Web.HttpContext.Current.Application["solarsearch_dbConnectionString"]" is not valid - it is returning as a null. At a guess, "solarsearch_dbConnectionString" is not in your configuration...
BTW: I would use System.Configuration.Appsettings["tag"]..
because it is applicable inside a class for both web
applications and windows applications. At some point,
you may need to use your business logic for the web
app on the desktop.
这篇关于填充数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文