ExecuteNonQuery,ExecuteReader和SqlDataAdapter插入查询3次

时间:2017-02-05 13:57:34

标签: c# asp.net sql-server

public static class db
{
    public static string databaseName = "blue_pumpkin";

    public static Temp query(string qr)
    {
        var s = new Temp();
        s.query(qr);
        return s;
    }
}

public class Con
{
    static SqlConnection sc = null;

    public static SqlConnection connect()
    {
        if (sc == null)
        {
            sc = new SqlConnection("server=localhost\\SQLEXPRESS;database=" + db.databaseName + ";Integrated Security=True;");
        }

        return sc;
    }
}

public class Temp
{
    private SqlConnection con;
    private string sqlQuery = "";
    private DataTable dataset = new DataTable();

    public long last_insert_id = -1;
    public int rows_affected = -1;
    public int num_rows = 0;
    public int num_columns = 0;
    public Boolean hasRows = false;

    public void query(string qr)
    {
        this.sqlQuery = qr;
        this.con = Con.connect();
        this.con.Open();

        SqlCommand cd = new SqlCommand(this.sqlQuery, this.con);

        try {
            this.rows_affected = cd.ExecuteNonQuery();
        } catch (Exception e) {
            this.rows_affected = -1;
        }

        SqlDataReader reader = cd.ExecuteReader();
        this.hasRows = reader.HasRows;
        reader.Close();

        SqlDataAdapter sda = new SqlDataAdapter(cd);
        sda.Fill(this.dataset);

        this.num_rows = this.dataset.Rows.Count;
        this.num_columns = this.dataset.Columns.Count;

        string sql = "SELECT SCOPE_IDENTITY();";
        SqlCommand cmd = new SqlCommand(sql, this.con);

        try {
            this.last_insert_id = Convert.ToInt64(cmd.ExecuteScalar());
        } catch (Exception e) {
            this.last_insert_id = -1;
        }

        this.con.Close();
    }

    public DataTable getDataTable()
    {
        return this.dataset;
    }
}

这就是我在需要的每个页面中使用它的方式。

var b = db.query("SELECT * FROM [" + db.databaseName + "].[dbo].[registration] ");
Response.Write("Last id : " + b.last_insert_id.ToString() + "<br>");
Response.Write("hasRows : " + b.hasRows.ToString() + "<br>");
Response.Write("num_columns : " + b.num_columns.ToString() + "<br>");
Response.Write("num_rows : " + b.num_rows.ToString() + "<br>");
Response.Write("rows_affected : " + b.rows_affected.ToString() + "<br>");

var c = db.query("UPDATE  [" + db.databaseName + "].[dbo].[registration] SET reg_password = 'Pune'" +
"WHERE reg_id = 1; ");
Response.Write("Last id : " + c.last_insert_id.ToString() + "<br>");
Response.Write("hasRows : " + c.hasRows.ToString() + "<br>");
Response.Write("num_columns : " + c.num_columns.ToString() + "<br>");
Response.Write("num_rows : " + c.num_rows.ToString() + "<br>");
Response.Write("rows_affected : " + c.rows_affected.ToString() + "<br>");

var queryString = "INSERT INTO [" + db.databaseName + "].[dbo].[registration] ([reg_name], [reg_age], [reg_gender], " +
   " [reg_creation_date], [reg_email], [reg_password], [reg_is_approved]) VALUES" +
   " ('"+ reg_name + "', '" + reg_age + "', '" + reg_gender + "', GETDATE(), '" + reg_email + "',"+
   " '" + reg_password + "', 0)";
db.query(queryString);

//OR 

DataTable dt = db.query("SELECT * FROM [" + db.databaseName + "].[dbo].[registration] WHERE [reg_id]=1").getDataTable();

一切正常,除非我运行插入查询,因为ExecuteNonQueryExecuteReaderSqlDataAdapter而插入3次。

如果我单独运行它们,它可以正常工作,但如果我一起运行它们,它们会插入3次。因为我想在同一个函数中运行所有类型的查询,有没有办法使它同时工作所有3个并且只插入一次查询插入而不影响更新或删除或不同的模式查询。

我能想到的一件事就是单独为插入数据创建insertQuery()函数。但是,如果有人能给我一个更好的想法而不是我的欢迎。

1 个答案:

答案 0 :(得分:1)

问题是在查询方法中,这三行正在执行insert语句。

    this.rows_affected = cd.ExecuteNonQuery();

    SqlDataReader reader = cd.ExecuteReader();

    sda.Fill(this.dataset);

您需要根据要执行的操作将这些执行分开,例如,如果该语句不是select,则仅执行 SqlCommand.ExecuteNonQuery 方法,否则执行 SqlDataAdapter。填充方法。

如果需要知道select语句是否存在行或行数,则可以使用以下代码,而无需使用SQLDataReader

    SqlCommand cd = new SqlCommand(sqlQuery, con);
    DataTable dataTable = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter(cd);
    sda.Fill(dataTable); 
    Boolean hasRows = (dataTable != null && dataTable.Rows.Count > 0);
    Int32 noRows = (hasRows ? dataTable.Rows.Count : 0);

希望对您有用!