填充selectcommand.connection属性尚未初始化 [英] Fill selectcommand.connection property has not been initialized

查看:115
本文介绍了填充selectcommand.connection属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码时,显示错误如下



fill selectcommand.connection属性尚未初始化



如何修复此错误。请告诉我。



我的尝试:



< pre lang =c#> if (e.CommandName == UploadFarmData
{

string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName) ;
constr = string .Format( @ Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0}; Extended Properties =Excel 12.0 Xml; HDR = YES;,CurrentFilePath);
Econ = new OleDbConnection(constr);
Econ.Open();
DataTable dtExcelSchema;
dtExcelSchema = Econ.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null );
string sheetName = dtExcelSchema.Rows [ 0 ] [ TABLE_NAME]。ToString();
DataSet ds = new DataSet();
string SheetName = dtExcelSchema.Rows [ 0 ] [ TABLE_NAME]。ToString();
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
cmdExcel.CommandText = SELECT * From [ + SheetName + ];
da.SelectCommand = cmdExcel;
da.Fill(ds);
Econ.Close();
DataTable dt = new DataTable();
dt = ds.Tables [ 0 ];
bool s = false ;
if (dt.Rows.Count > 0
{
for int i = < span class =code-digit> 0 ; i < dt.Rows.Count; i ++)
{
dal .DAL_Insert_Farm_Data(dt.Rows [i] [ Farmer Code]。ToString(),dt。行[i] [ 农场否]。ToString(),
dt.Rows [i] [ 农场类型]。ToString(),dt.Rows [i] [ 种子日期]。ToString(),dt.Rows [i] [ 农场访问否]。ToString());
s = true ;
}


如果(s)
{
showStatusTrue.InnerHtml = 农场详细信息已成功上传;
dal.DAL_Data_Update();
}

解决方案

查看错误消息:

Quote:

填充selectcommand.connection属性尚未初始化

并且它没有。

你创建一个Connection对象,你甚至打开它:

 Econ = new OleDbConnection(constr); 
Econ.Open();

但这就是你所做的 - 你永远不会将它连接到你的DataAdapter。

使用参数化构造函数:

 OleDbDataAdapter da = new OleDbDataAdapter(SELECT * From [+ SheetName +],Econ); 
da.Fill(ds);

但要小心:当你连接字符串以形成SQL命令时,你会让自己对SQL注入开放,这可能会损坏或破坏你的数据库。除非您完全控制 SheetName 的内容,否则此代码的风险很高。

BTW:您不需要打开和关闭连接对象来使用DataAdapter - 如果需要,它将为您执行此操作。


尝试替换:

 OleDbCommand cmdExcel = new OleDbCommand(); 
cmdExcel.CommandText =SELECT * From [+ SheetName +];



with:

  string  sql =   SELECT * From [  + SheetName +   


;
OleDbCommand cmdExcel = new OleDbCommand(sql,Econ);



因为:

1. OleDbCommand必须与OleDbConnection对象相关。

2.必须有[


when run the below code, it shows error as follows

fill selectcommand.connection property has not been initialized

how to fix this error. please let me know.

What I have tried:

if (e.CommandName == "UploadFarmData")
                {

  string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
                        constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);
                        Econ = new OleDbConnection(constr);
                        Econ.Open();
                        DataTable dtExcelSchema;
                        dtExcelSchema = Econ.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                        DataSet ds = new DataSet();
                        string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                        OleDbCommand cmdExcel = new OleDbCommand();
                        OleDbDataAdapter da = new OleDbDataAdapter();
                        cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; 
                        da.SelectCommand = cmdExcel;
                        da.Fill(ds);
                        Econ.Close();
                        DataTable dt = new DataTable();
                        dt = ds.Tables[0];
                        bool s = false;
                        if (dt.Rows.Count > 0)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                dal.DAL_Insert_Farm_Data(dt.Rows[i]["Farmer Code"].ToString(), dt.Rows[i]["Farm No"].ToString(),
                                    dt.Rows[i]["Type of Farm"].ToString(), dt.Rows[i]["Seeded Date"].ToString(), dt.Rows[i]["Farm Visit No"].ToString());
                                s = true;
                            }


                            if (s)
                            {
                 showStatusTrue.InnerHtml = "Farm Details Uploaded Successfully";
                            dal.DAL_Data_Update();
                            }

解决方案

Look at the error message:

Quote:

Fill selectcommand.connection property has not been initialized

And it hasn't.
You create a Connection object, you even Open it:

Econ = new OleDbConnection(constr);
Econ.Open();

But that's all you do - you never connect that to your DataAdapter.
Use the parameterized constructor instead:

OleDbDataAdapter da = new OleDbDataAdapter("SELECT * From [" + SheetName + "]", Econ);
da.Fill(ds);

But be damn careful: when you concatenate strings to form SQL commands, you leave yourself open to SQL Injection which can damage or destroy your DB. Unless you have absolute control over the contents of SheetName you are at a high risk with this code.
BTW: you don't need to Open and Close a connection object to work with a DataAdapter - it will do that for you if it needs to.


Try to replace:

OleDbCommand cmdExcel = new OleDbCommand();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";


with:

string sql = "SELECT * From [" + SheetName + "


"; OleDbCommand cmdExcel = new OleDbCommand(sql, Econ);


because:
1. OleDbCommand have to be related to OleDbConnection object.
2. There's obligatory [


这篇关于填充selectcommand.connection属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆