2016-04-18 5 views
0

箱。さがすには、私がこれまでのところ、私はビューを持っているADO.NET</p> <p>を通じて検索ボックス機能を引き出すためにしようとしているテーブルado.net

public ActionResult NoEF(string searchKey) 
    { 
     Pokemon poke = new Pokemon(); 

      poke.Search(searchKey); 
     //poke.Search(poke.Poke_Name, poke.Poke_Color); 

     return View(poke); 

    } 

Pokemon.csファイルにリダイレクトすることになっている: は、それから私は、コントローラを持っています。それはそうのように、検索ボックスに入力します、データベースに接続し、クエリを実行し、それに基づいて情報を表示するようになっていたクラスがあります。

public List<Pokemon> Search(string searchKey) 
     { 
      List<Pokemon> pokemons = new List<Pokemon>(); 


      SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename = 'C:\Users\Pokedatabase.mdf'; Integrated Security = True"); 

      //Default order by name 
      string query = "SELECT  Pokemon.*, Poketype.* FROM Pokemon INNER JOIN" + 
          " Poketype ON Pokemon.P_Type = Poketype.Type_Id" + 
          " Where Pokemon.Poke_Name = '"+ searchKey + "' || Poke_Color = '"+ searchKey + "' order by Poke_Name"; 

      //Opens up a connection to the database 
      con.Open(); 

私の問題は、接続を開いた後、私はということです何をそこに置くべきかについてはあまり確かではありません。私はどこにでもgoogledした。これまで明らかに機能しないYOLOコードを使用しています。

using (SqlCommand cmd = new SqlCommand(query, con)) 
    { 
     //Creates a data reader object and supplies it with data 
     SqlDataReader dr = cmd.ExecuteReader(); 

     try 
     { 
      con.Open(); 
      while (dr.Read()) 
      { 
       dr = dr(dr.GetValue(0) + " - " + dr.GetValue(1) + " - " + dr.GetValue(2)); 
      } 
      dr.Close(); 
      cmd.Dispose(); 
      con.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Can not open connection ! "); 
     } 

    } 

    return pokemons; 
} 

改良点はありますか?私は完全に失われています。

+0

何:これはとポケモンの種類のリストを記入した後、

 con.open(); sqlDs = new DataSet(); conOpen(); var sqlComm = new SqlCommand(); sqlComm.Connection = con; sqlComm.CommandType = CommandType.Text; sqlComm.CommandText = query; sqlDa = new SqlDataAdapter(sqlComm); sqlDa.Fill(sqlDs); conClose(); var dt = new DataTable(); dt = sqlDs.Tables[0]; 

あなたの 'ポケモン'クラスはどのように見えるのですか?データベースから設定しようとしているプロパティは何ですか? – Tim

+0

'パブリック部分クラスポケモン { public int Poke_Id {get;セット; } 公開ストリングPoke_Name {get;セット; } 公開ストリングPoke_Color {get;セット; } public int Poke_Hp {get;セット; } public int P_Type {get;セット; } public仮想Poketype Poketype {get;セット; } ' – Rinn

+0

ADO.NETでは、あなた自身の' SqlDataReader'からすべてのプロパティを設定する必要があります。 'Poke_Name = dr [" Poke_Name "];'などとなります。 – Tim

答えて

0

のようなポケモンのクラスを作成します。あなたがあなたのデータのように好きなプロパティを追加することができます

public class Pokemon 
    { 
     public string Name{get;set;} 
     public string Type{get;set;} 
    } 

を、私は助けるためとして、それらの2つだけを取りました。検索機能でより た後、次のように実行します。

 var list = (from a in dt 
        select new Pokemon 
        { 
         Name = a.Name, 
         Type = a.Type 
        }).ToList(); 

      return list; 

検索機能のコントローラへの復帰このリストを

+0

@Rinnはあなたのために働いた彼のソリューションですか? – sumngh

関連する問題