简体   繁体   English

如何使用C#ASP.net返回SQL查询结果消息

[英]How to return sql query result messages with c# ASP.net

I tried following code but I couldn't find success messages from it.Here commandText can be insert,update,delete or select query and I need dataset if it is a select query else success messages like in sql result output("12 row(s) inserted successfully") .I can't use ExecuteScalar or ExecuteNonQuery methods since I need dataset output when select query executed. 我尝试了以下代码,但无法从中找到成功消息。在这里,可以将commandText插入,更新,删除或选择查询,如果需要选择查询,则需要数据集,否则需要成功消息,如sql结果output("12 row(s) inserted successfully") 。我无法使用ExecuteScalarExecuteNonQuery方法,因为执行选择查询时需要数据集输出。

public static  DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
        using (SqlConnection cn = new SqlConnection(connectionString))
        {
            cn.InfoMessage += delegate(object sender, SqlInfoMessageEventArgs e)
            {
                MessageInfo += "\n" + e.Message;
            };
            cn.FireInfoMessageEventOnUserErrors = true;

            cn.Open();
            using (SqlCommand command = new SqlCommand(commandText, cn))
            {

                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    DataSet dt = new DataSet();
                    adapter.Fill(dt); // Do something with DataTable
                    return dt;
                }
            }
        }
    }

I am using System.Data version 4.0.0.0 and the following works for me with UPDATE, INSERT, DELETE and SELECT statements. 我正在使用System.Data版本4.0.0.0,以下代码对我有用:UPDATE,INSERT,DELETE和SELECT语句。

using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
    var rowsAffected = -1;

    adapter.SelectCommand.StatementCompleted += delegate (object sender, StatementCompletedEventArgs e)
    {
        rowsAffected = e.RecordCount;
    };

    DataSet dt = new DataSet();
    adapter.Fill(dt); // Do something with DataTable
    return dt;
}

I managed to solve it by following way but hope there should be a better way. 我设法通过以下方式解决了该问题,但希望应该有更好的方法。

 string type = Query.Trim().Split(' ')[0].ToLower();
    if ( type == "select")
    {
        DataSet result = SqlHelper.ExecuteDataset(CommandType.Text, Query);                

    }
    else
    {                
        if(type == "insert") type="inserted."; else type=type+"d.";
        int a=(int)SqlHelper.ExecuteNonQuery(CommandType.Text, Query);
        if (a > 0)
        {
            lblInfo.Text = "Query executed successfully. " + a.ToString() + " row(s) " + type;                

        }
    }

I found another one.That's better than above one. 我找到了另一个,那比上面的要好。

query = resultQuery + "  SELECT '('+CONVERT(VARCHAR(10),@@ROWCOUNT)+' row(s) affected)' AS RESULT";
DataSet result = DataSet result = SqlHelper.ExecuteDataset(CommandType.Text, query);

        if (result.DefaultViewManager.DataViewSettingCollectionString.Contains("Table") == true)
        {
            sqlGrid.Visible = true;
            sqlGrid.DataSource = result;
            sqlGrid.DataBind();
        }
        if (sqlGrid.Rows.Count==1 && sqlGrid.Rows[0].Cells.Count==1)
        {
            string text = (sqlGrid.Rows[0].Cells[0]).Text;
            if (text.Contains("row(s) affected"))
            {
                lblInfo.Text=text;
                lblInfo.Visible=true;
                sqlGrid.DataSource = null;
                sqlGrid.DataBind();
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM