Tech Neo 创造一个纯粹高质的技术交流平台

求助:ADO.NET 无法将void类型转换成datatable 类型

<div class="cnblogs_code">
<pre><span style="color: #0000ff">namespace</span><span style="color: #000000"> DAL
{
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> xueshengbiaos

{




 </span><span style="color: #0000ff">public</span>  xueshengbiaos xs(<span style="color: #0000ff">string</span><span style="color: #000000"> username)
 {






     </span><span style="color: #0000ff">string</span> sql = <span style="color: #800000">"</span><span style="color: #800000">select *  from xueshengbiaos where usernames=@username</span><span style="color: #800000">"</span><span style="color: #000000">;
     List</span><SqlParameter> pra = <span style="color: #0000ff">new</span> List<SqlParameter><span style="color: #000000">();
     pra.Add(</span><span style="color: #0000ff">new</span> SqlParameter(<span style="color: #800000">"</span><span style="color: #800000">@usernames</span><span style="color: #800000">"</span><span style="color: #000000">, username));
    DataTable dt</span>=<span style="color: #000000"> SqlHelper.ExecuteDataTable(sql, pra);




    
 
 }
 


}

}</span></pre>
</div>
<p>SqlHelper部分</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff">using</span><span style="color: #000000"> System;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Collections.Generic;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Linq;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Data;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Data.SqlClient;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Configuration;

</span><span style="color: #0000ff">namespace</span><span style="color: #000000"> DAL
{

</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> SqlHelper
{
    </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> <span style="color: #0000ff">string</span> connString = ConfigurationManager.ConnectionStrings[<span style="color: #800000">"</span><span style="color: #800000">name</span><span style="color: #800000">"</span><span style="color: #000000">].ConnectionString;
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> ExecuteNonQuery(<span style="color: #0000ff">string</span> sql,<span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters )
    {
        </span><span style="color: #0000ff">using</span> (SqlConnection conn = <span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection(connString))
        {
            conn.Open();
            </span><span style="color: #0000ff">using</span> (SqlCommand cmd = <span style="color: #0000ff">new</span><span style="color: #000000"> SqlCommand(sql, conn))
            {
               cmd.Parameters.AddRange(parameters);
               </span><span style="color: #0000ff">return</span><span style="color: #000000"> cmd.ExecuteNonQuery();
            }
        }
    }
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">object</span> ExecuteScalar(<span style="color: #0000ff">string</span> sql, <span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters)
    {
        </span><span style="color: #0000ff">using</span> (SqlConnection conn = <span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection(connString))
        {
            conn.Open();
            </span><span style="color: #0000ff">using</span> (SqlCommand cmd = <span style="color: #0000ff">new</span><span style="color: #000000"> SqlCommand(sql, conn))
            {
                cmd.Parameters.AddRange(parameters);
                </span><span style="color: #0000ff">return</span><span style="color: #000000"> cmd.ExecuteScalar();
            }
        }
    }
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> DataTable ExecuteDataTable(<span style="color: #0000ff">string</span> sql, <span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters)
    {
        DataSet </span><span style="color: #0000ff">set</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> DataSet();
        </span><span style="color: #0000ff">using</span> (SqlConnection conn = <span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection(connString))
        {
            conn.Open();
            </span><span style="color: #0000ff">using</span> (SqlCommand cmd = <span style="color: #0000ff">new</span><span style="color: #000000"> SqlCommand(sql, conn))
            {
                </span><span style="color: #0000ff">if</span> (parameters!=<span style="color: #0000ff">null</span><span style="color: #000000">)
                {
                    cmd.Parameters.AddRange(parameters); 
                }
                SqlDataAdapter adapter </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> SqlDataAdapter(cmd);
                adapter.Fill(</span><span style="color: #0000ff">set</span><span style="color: #000000">);
                </span><span style="color: #0000ff">return</span> <span style="color: #0000ff">set</span>.Tables[<span style="color: #800080">0</span><span style="color: #000000">];
            }
        }
    }

    </span><span style="color: #0000ff">internal</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> ExecuteDataTable(<span style="color: #0000ff">string</span> sql, List<SqlParameter><span style="color: #000000"> pra)
    {
        </span><span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span><span style="color: #000000"> NotImplementedException();
    }
}

}</span></pre>
</div>

请先 登录 后评论

最佳答案 2018-06-14 16:50

return set.Tables[0]; 估计set或者set.Tables[0]是null,需要对应的地方检查null,否则在null上调用方法肯定异常

请先 登录 后评论

其它 3 个回答

博问用户

用的是第二个无返回值的静态方法:internal static void ExecuteDataTable

请先 登录 后评论
博问用户

<div class="cnblogs_code">
<pre> <span style="color: #0000ff">internal</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> ExecuteDataTable(<span style="color: #0000ff">string</span> sql, List<SqlParameter><span style="color: #000000"> pra)

{
  </span><span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span><span style="color: #000000"> NotImplementedException();
}</span></pre>

</div>
<p>这个有返回值吗?</p>

请先 登录 后评论
博问用户

你调的是 void ExecuteDataTable(string sql, List<SqlParameter> pra) 这个方法,它确实返回void(就是每返回值),所以无法用 DataTable类型的变量来引用。

请先 登录 后评论