数据集返回空 [英] Dataset returns empty

查看:80
本文介绍了数据集返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这段代码是否正确,返回值时我的数据集显示为空.

Hi,

This code is correct or not ,at the time of return the value my dataset shows the empty.

DataSet dset;
        SqlDataAdapter dAdapter;
        SqlCommand command = new SqlCommand();
        //string deptno = "";
        try
        {
            con.Open();
            dAdapter = new SqlDataAdapter(spNameForXlUpload, con);
            command.CommandType = CommandType.StoredProcedure;
            command.Connection = con;
            command.CommandText = spNameForXlUpload;
            command.Parameters.Add(new SqlParameter("@XMLItemsDoc", xmlDoc.Trim()));
            
            command.ExecuteNonQuery();
            
            dAdapter.SelectCommand = command;
            dset = new DataSet();
            dAdapter.Fill(dset);
            return dset;
        }



谁能帮助我做到这一点


谢谢..



can anyone help me to do this


Thanks..

推荐答案

看看下面的代码

Have a look on the Below code

Public DataSet GetDataformSP()
{
string connString = "<your connection="" string="">";
string sql = "name of your sp";

SqlConnection conn = new SqlConnection(connString);

try {
   SqlDataAdapter da = new SqlDataAdapter();
   da.SelectCommand = new SqlCommand(sql, conn);
   da.SelectCommand.CommandType = CommandType.StoredProcedure;
//Add parameters here as you stated in your question.    
//command.Parameters.Add(new SqlParameter("@XMLItemsDoc", xmlDoc.Trim()));

   DataSet ds = new DataSet();   
   da.Fill(ds, "result_name");

  return ds;

} catch(Exception e) {
   Console.WriteLine("Error: " + e);
} finally {
   conn.Close();
}
}</your>



如果您的sp返回记录集,则仅此方法有效.否则,您可以使用命令对象的ExecuteNoneQuery功能.试试这个:
Hi,
If your sp returns recordset then only this will work. Otherwise you can use ExecuteNoneQuery function of command object. Try this:
DataSet dset = null;
SqlDataAdapter dAdapter;
//string deptno = "";
try
{
    con.Open();
    SqlCommand command = new SqlCommand(spNameForXlUpload, con);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add(new SqlParameter("@XMLItemsDoc", xmlDoc.Trim()));
    dAdapter = new SqlDataAdapter(command);        
    dset = new DataSet();
    dAdapter.Fill(dset);
    return dset;
}




祝一切顺利.
--Amit




All the best.
--Amit


这篇关于数据集返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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