2014-09-03 61 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