我在Postgresql中使用.Net提供程序,当我运行以下代码时,我得到“对象引用未设置为对象的实例”。错误。
DataTable x = new DataTable();
x = cmdGetRow("select * from employee;"); // I get the error here
public DataTable cmdGetRow(String SQL)
{
NpgsqlDataAdapter NpAdapter = new NpgsqlDataAdapter();
DataSet dset = new DataSet("hr");
NpAdapter.SelectCommand = new NpgsqlCommand(SQL, dbConnection);
NpAdapter.Fill(dset, "employee");
return dset.Tables["employee"];
}
你能帮帮我吗?
答案 0 :(得分:0)
尝试:
DataTable x = new DataTable();
cmdGetRow("select * from employee;", ref x);
public void cmdGetRow(String SQL, ref table)
{
NpgsqlDataAdapter NpAdapter = new NpgsqlDataAdapter();
DataSet dset = new DataSet("hr");
NpAdapter.SelectCommand = new NpgsqlCommand(SQL, dbConnection);
NpAdapter.Fill(dset, "employee");
table = dset.Tables["employee"];
}
答案 1 :(得分:0)
也许这可以帮助您:在从数据库获取数据之前,确保存在连接并且连接已打开。
public DataTable cmdGetRow(String SQL)
{
string conString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
_server, _port, _userId, _password, _database);
NpgsqlConnection connection = new NpgsqlConnection(conString);
connectin.Open();
NpgsqlDataAdapter command = new NpgsqlDataAdapter(sql, connection);
DataSet dataSet = new Dataset();
dataSet.Reset();
command.Fill(dataSet);
return dataSet.Tables[0];
}