【问题标题】:Cannot return datatable无法返回数据表
【发布时间】:2010-10-19 22:21:11
【问题描述】:
public DataTable ExcelToDatatable_dt1  
{
    get  
    {  
        foreach (GridViewRow rowItem in GridView1.Rows)  
        {  
            CheckBox chk;  
            // gets the Debit Reference number for each row checked  
            string productId = GridView1.DataKeys[rowItem.RowIndex].Value.ToString();  
           chk = (CheckBox)(rowItem.Cells[0].FindControl("RowChecker"));  
           if (chk.Checked)  

           {  
               string fileName = Session["fileName"].ToString();  
               //string fileName = "Collection.csv";  
               // put the file name here  
               string strSql = "SELECT * FROM [" + fileName + "] WHERE DDIREF = ["+ productId +]";  
               string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".\\Files\\") + ";" + "Extended Properties='text;HDR=YES;'";  


               using (FileStream filestr = new FileStream(Server.MapPath(".\\Files\\") + "\\schema.ini",  
                   FileMode.Create, FileAccess.Write))  
               {  
                   using (StreamWriter writer = new StreamWriter(filestr))  
                   {  
                       writer.WriteLine("[" + fileName + "]");  
                       writer.WriteLine("ColNameHeader=False");  
                       writer.WriteLine("Format=CSVDelimited");  
                       writer.WriteLine("DateTimeFormat=dd-MMM-yy");  

                       //Message_ltr.Text += positionList[0].ToString();  

                       for (int i = 0; i < 29; i++)  
                           //if (((positionList[i].ToString() != null) && (positionList[i].ToString() != "")) && ((nameList[i].ToString() != null) && (nameList[i].ToString() != "")))  
                           writer.WriteLine("Col" + positionList[i].ToString() + "=" + nameList[i].ToString() + " Text Width 100");  
                       //writer.WriteLine("Col2=SortCode Text Width 30");  

                       writer.Close();  
                       writer.Dispose();  
                   }  
                   filestr.Close();  
                   filestr.Dispose();  
               }  


               OleDbDataAdapter oleda = new OleDbDataAdapter(strSql, strCSVConnString);  
               DataTable dtbCSV = new DataTable();  
               oleda.Fill(dtbCSV);  

               return dtbCSV;  
           }  

        }  

        return null;// instead of null I want to return dtbCSV here  
        }  

这会报错

“错误 45 'DirectDebit.Interbacs.CompanyUpload.ExcelToDatatable_dt1.get':并非所有代码路径都返回值”。

错误是由于返回语句不在正确的范围内。但问题是,我想返回 dtbCSV 并且我无法在“get”范围内返回它,因为 dtCSV 在 get 上下文中不存在,我不知道在返回方法中的范围之间传递值。

【问题讨论】:

    标签: c# excel csv checkbox datagridview


    【解决方案1】:

    尝试将 DataTable dtbCSV = new DataTable(); 放在您的 foreach 语句之前(并删除其他声明)。

    编辑
    注意:这会将dtbCSV 放在整个get 块的范围内,而不仅仅是在您的if 语句中。

    【讨论】:

    • 您好 thnx 整理了这个问题 .. 真的很感激。我的查询还有一个问题,在上面的示例中,文件名是 .csv 文件。 sql 查询与 "Select * from ["+ file name +" 一起工作正常,但是当我使用 WHERE DDRIEF= product id; 时它会引发错误
    • 错误:没有为一个或多个必需参数提供值
    • 您在第二个 + 后缺少双引号:["+ productId +]"; 将其更改为:["+ productId + "]"; 看看是否有帮助
    • 其实多看几遍,应该去掉product ID两边的方括号。方括号将使它像一列一样引用它(除非那是你想要的)。你真的应该为此使用参数。见这里:msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    相关资源
    最近更新 更多