一个命令的多个查询一个结果

时间:2014-09-03 10:22:32

标签: c# sql .net

我正试图从这个命令中得到结果它应该返回一些谎言 (X)受影响的行 命令已成功完成 打印声明

我不知道如何在richtextbox中获得这些结果。如果有人在那里可以帮助我会很酷

sqlconn.cnn = new SqlConnection("Data Source=.\\SQLEXPRESS" + ";Initial Catalog=" + shard.Text + ";User ID=" + textBox3.Text + ";Password=" + textBox4.Text);
sqlconn.cnn.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM _SiegeFortress Where FortressID LIKE'%[1,3,6]%' INSERT INTO _SiegeFortress (FortressID,GuildID,TaxRatio,Tax,NPCHired,TempGuildID,Introduction,CreatedDungeonTime,CreatedDungeonCount,IntroductionModificationPermission) VALUES (1 ,0 ,0 ,0 ,0 ,0 ,NULL ,NULL ,0 ,1) INSERT INTO _SiegeFortress (FortressID,GuildID,TaxRatio,Tax,NPCHired,TempGuildID,Introduction,CreatedDungeonTime,CreatedDungeonCount,IntroductionModificationPermission) VALUES (3 ,0 ,0 ,0 ,0 ,0 ,NULL ,NULL ,0 ,1) INSERT INTO _SiegeFortress (FortressID,GuildID,TaxRatio,Tax,NPCHired,TempGuildID,Introduction,CreatedDungeonTime,CreatedDungeonCount,IntroductionModificationPermission) VALUES (6 ,0 ,0 ,0 ,0 ,0 ,NULL ,NULL ,0 ,1) PRINT 'fewfwef'", sqlconn.cnn);
cmd.Connection = sqlconn.cnn;
SqlDataReader crap;

        try
        {
            crap = cmd.ExecuteReader();
            crap.Close();
            MessageBox.Show("Crap");
            SqlDataReader name = cmd.ExecuteReader();

            richTextBox1.Text = name.ToString();
            name.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

1 个答案:

答案 0 :(得分:1)

您的DELETE查询未返回任何数据。将ExecuteReaderDELETE声明一起使用是没有意义的。

您需要使用ExecuteNonQuery代替ExecuteReader

来自documentation;

  

对于UPDATE,INSERT和DELETE语句,返回值为   受命令影响的行数。

还可以使用using statement来处置您的SqlConnectionSqlCommand

using(SqlConnection sqlconn = new SqlConnection(ConnectionString))
using(SqlCommand cmd = conn.CreateCommand())
{
    sqlconn.Open();
    richTextBox1.Text = string.Format("{0} rows affected", 
                                      (string)cmd.ExecuteNonQuery());
}