gpt4 book ai didi

c# - 填充 : SelectCommand. 连接属性尚未初始化。该怎么办?

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:25 24 4
gpt4 key购买 nike

当我尝试使用方法 GetCities() 时,它说:

Fill: SelectCommand.Connection property has not been initialized.

谁能告诉我该怎么做?

public class CitiesService
{
public DataSet DS;
public OleDbConnection myConnection;
public OleDbDataAdapter adapter;
public OleDbDataAdapter adapter2;

public CitiesService()
{
}

public DataSet GetCities()
{
OleDbCommand myCmd = new OleDbCommand("SPtblCities", myConnection);
myCmd.CommandType = CommandType.StoredProcedure;
OleDbDataAdapter Adapter = new OleDbDataAdapter();
Adapter.SelectCommand = myCmd;
DataSet dataSet = new DataSet();

try
{
Adapter.Fill(dataSet, "tblCities");
dataSet.Tables["tblCities"].PrimaryKey = new DataColumn[]
{
dataSet.Tables["tblCities"].Columns["CityID"]
};
}
catch (OleDbException ex)
{
throw ex;
}

return dataSet;
}

}

最佳答案

是的,连接没有初始化,它是null

替换

public OleDbConnection myConnection;

public OleDbConnection myConnection = new OleDbConnection(ConnectionString);

或者在方法中:

myConnection = new OleDbConnection(ConnectionString);
OleDbCommand myCmd = new OleDbCommand("SPtblCities", myConnection);

关于c# - 填充 : SelectCommand. 连接属性尚未初始化。该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21141350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com