2016-07-28 5 views
-1

私はC#を初めて使用しており、[File_ID]を要求する2つのテキストボックスを持つ小さなフォームアプリケーションを作成する必要があります。その番号をクエリに送信し、別のテキストボックスには出力を表示します。SQLクエリに基づいてテキストボックスのテキストを別のテキストボックスに追加するC#ボタン

私はこれで少し遊んでいて、このようなものを持っています。しかし、それは動作していません。私は別の方向に進むべきかどうか分からない。私は本当にあなたの助けに感謝します。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace testing 
{ 
    public partial class Form1 : Form 
    { 
     String str,dr; 
     SqlConnection con = new SqlConnection("Data Source=USHOU2016\\USFi;Initial Catalog=HOU_2016_Project;Integrated Security=True"); 
     SqlCommand cmd; 
     public Form1() 
     { 
      InitializeComponent(); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      con.Open(); 
      str = "SELECT TOP 1,[Sender Name],[Subject] from [OLE DB Destination] WHERE [CHAT #] ='" + textBox1.Text + "'"; 

      cmd = new SqlCommand(str, con); 
      // dr = cmd.ExecuteReader(); 
      con.Close(); 

     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void textBox2_TextChanged(object sender, EventArgs e) 
     { 
      textBox1.Text = cmd.ExecuteScalar().ToString(); 

     } 


    } 
} 

答えて

0

あなたのSQLクエリの構文は間違っています。あなたはTOP 1後に余分な,を持っている...あなたのボタンで再び

SELECT TOP 1 [Sender Name],[Subject] from [OLE DB Destination] WHERE... 

は、あなただけのコマンドcmd = new SqlCommand(str, con);を作成しませんが、決してそれを実行していることをクリックしてください削除します。接続を閉じるだけです。

textBox2_TextChangedイベントハンドラでは、クエリを実行しようとしていますが、接続はすでに終了しています。私はADO.NETについて読んでみるべきだと思います。

+0

ありがとうございます。私はちょうど私のSQLデータをフォーム形式で出力するためのツールとしてC#が必要になります。私はcmd = new SqlCommand(str、con)をどのように実行しますか?テキストボックス1に何かを追加する必要がありますか? –

0

これはすべきことです。物事のカップルは注意する:

  1. ボタンがあなたのSQLを実行し、第二テキストボックスを移入されているので、あなたのSQLクエリにあなたの変数を追加する文字列の連結を使用してtextbox_changedイベント

  2. ための必要はありません悪い習慣であり、あなたのコードがSQLインジェクションの影響を受けやすいようにします。代わりに、下のコードに示すようにSQL入力をパラメータ化します。

    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
        InitializeComponent(); 
    } 
    
    private void button1_Click(object sender, EventArgs e) 
    { 
        string query = "SELECT TOP 1,[Sender Name],[Subject] " 
            + " from[OLE DB Destination] WHERE[CHAT #] = :chatId "; 
        using (SqlConnection con = new SqlConnection("Data Source=USHOU2016\\USFi;Initial Catalog=HOU_2016_Project;Integrated Security=True")) 
        { 
         SqlCommand cmd = new SqlCommand(query, con); 
         cmd.Parameters.AddWithValue("chatId", textBox1.Text); //use Sql parameters to protect yourself against Sql Injection! 
         con.Open(); 
    
         SqlDataReader reader = cmd.ExecuteReader(); 
    
         if (reader.HasRows) 
         { 
          reader.Read(); 
          textBox2.Text = reader[0] + " " + reader[1]; //or however you want your output formatted 
         } 
    
         reader.close(); 
        } 
    } 
    } 
    
関連する問題