2014-09-03 64 views
0

好吧,我想获得的结果出来这个命令应该返回的东西谎言 (X)行受到影响 命令成功完成 print语句这里多个查询

我不知道如何获得在richtextbox..so的结果,如果有任何人能帮助我,这将是冷静

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

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()); 
} 
+0

有一个插入/ print语句 – ex0ff 2014-09-03 10:38:33

+0

,当我使用的ExecuteNonQuery它返回一个int – ex0ff 2014-09-03 10:42:36

+0

@KhaledMohamed什么是'print'说法? o.O是的'ExecuteNonQuery'返回'int',你可以象我一样将它转换为'string'。 – 2014-09-03 10:44:00