2015-01-07 3 views
-1

내 인사말을 all.well에 mysql 데이터베이스에 데이터를 저장하는 데 lables 및 textboxes가 있고 콤보 상자를 추가하여 2 열 ID를 표시하는 저장 프로 시저의 정보를 검색합니다 및 이름. 내 질문은 어떻게 내 저장된 procrdures 결과 내 콤보 1 채 웁니다. 아래 sp_clearing_agent() 가 SELECT clearing_agent_id, clearing_agent_name 을 BEGIN 내 C# 코드 콤보 상자를 mysql 데이터베이스 저장 프로 시저로 채우는 방법

DROP PROCEDURE IF가 sp_clearing_agent $$

가 DEFINER을 만들 EXISTS sms_pigen $$

DELIMITER $$ 

USE = root PROCEDURE localhost @이고 FROM tms_clearing_agents; END $$

DELIMITER; 여기

public partial class frmNewClient : Form 
{ 


    MySqlConnection connection; 
    MySqlCommand cmd; 
    MySqlDataAdapter adp; 
    DataSet cb = new DataSet(); 
    public frmNewClient() 
    { 
     InitializeComponent(); 
    } 

    private void frmNewClient_Load(object sender, EventArgs e) 
    { 
    // var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString; 

    // connection = new MySqlConnection(connectionString); 
    // connection.Open(); 

    // DataSet cb = new DataSet(); 




    } 




    private void btnSubmitClients_Click_1(object sender, EventArgs e) 
    { 


     var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString; 

     connection = new MySqlConnection(connectionString); 
     connection.Open(); 

     try 
     { 

      string CmdText = "INSERT INTO t_pi_Clients(ClientCode,ClientName,PostalAdd,Telephone,Fax,EmailAdd1,EmailAdd2,EmailAdd3,Website,TotalDeposit,AccountBal,ChargeRate)VALUES(@ClientCode, @ClientName, @PostalAdd, @Telephone, @Fax, @EmailAdd1, @EmailAdd2, @EmailAdd3, @Website, @TotalDeposit, @AccountBal, @ChargeRate)"; 
      MySqlCommand cmd = new MySqlCommand(CmdText, connection); 
      cmd.Parameters.AddWithValue("@ClientCode", txtboxClientCode.Text); 
      cmd.Parameters.AddWithValue("@ClientName", txtboxClientName.Text); 
      cmd.Parameters.AddWithValue("@PostalAdd", txtboxPostalAddress.Text); 
      cmd.Parameters.AddWithValue("@Telephone", txtboxTelephone.Text); 
      cmd.Parameters.AddWithValue("@Fax", txtboxFax.Text); 
      cmd.Parameters.AddWithValue("@EmailAdd1", txtboxEmailAddress1.Text); 
      cmd.Parameters.AddWithValue("@EmailAdd2", txtboxEmailAddress2.Text); 
      cmd.Parameters.AddWithValue("@EmailAdd3", txtboxEmailAddress3.Text); 
      cmd.Parameters.AddWithValue("@Website", txtboxWebsite.Text); 
      cmd.Parameters.AddWithValue("@TotalDeposit", txtboxTotalDepo.Text); 
      cmd.Parameters.AddWithValue("@AccountBal", txtboxAccountBal.Text); 
      cmd.Parameters.AddWithValue("@ChargeRate", txtboxChargeRate.Text); 

      int result = cmd.ExecuteNonQuery(); 
      MessageBox.Show("Entry Saved"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
      return; 
     } 

답변

0

에 데이터를 바인딩하는 코드입니다. CommandType을 StoredProcedure로 설정하고이 명령을 SelectCommand로 MySqlDataAdapter에 전달하는 것이 중요합니다. 채우기로 데이터를 검색 한 후 콤보 상자에 대한 데이터 소스로 DataTable을 설정하고 DisplayMember 및 ValueMember를 테이블의 해당 필드로 설정합니다.

private void frmNewClient_Load(object sender, EventArgs e) 
{ 
    var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString; 
    using(MySqlConnection cnn = new MySqlConnection(connectionString)) 
    using(MySqlCommand cmd = cnn.CreateCommand()) 
    { 
     cnn.Open(); 
     cmd.CommandText = "sp_clearing_agent"; 
     cmd.CommandType = CommandType.StoredProcedure; 
     DataTable dt = new DataTable(); 
     using(MySqlDataAdapter da = new MySqlDataAdapter(cmd)) 
     { 
      da.Fill(dt); 
      combobox.DisplayMember = "clearing_agent_name"; 
      combobox.ValueMember = "clearing_agent_id"; 
      combobox.DataSource = dt; 

     } 
    } 
} 

연결 및 어댑터와 같이 전역 개체를 보관하지 마십시오. 당신이 그들을 필요로 할 때 즉시 그들을 파괴하고 사용 후 즉시 그들을 파괴하십시오.

+0

UR 노력과 피드백 덕분에 제가 의견을 제시하려고 노력할 것입니다 .... 나는 또한 그것을 마음에 간직 할 것입니다. – RichieCr7

+1

다시 한 번 U. .. 내가 너에게 많은 것을 배웠다. – RichieCr7

0

는 저장 프로 시저에서 데이터를 검색하고 연결과의 StoredProcedure를 통해 데이터를 검색하는 명령을 만들려면의 Form_Load 명령에서으로 comboBox

 DataTable dtTest = new DataTable(); 
     SqlConnection con=new SqlConnection(ConnectionString); 
     con.Open(); 
     SqlCommand cmd=new SqlCommand(); 
     cmd.CommandText="sp_clearing_agent"; 
     cmd.CommandType=CommandType.StoredProcedure; 

     SqlDataAdapter sdaTest = new SqlDataAdapter(cmd); 
     DataSet dsTest=new DataSet(); 
     sdaTest.Fill(dsTest); 
     //binding data to combobox 
     cmbTest.DataSource = dsTest.Tables[0]; 
     comboBox1.DataSource.DisplayMember="clearing_agent_id" 
     comboBox1.DataSource.ValueMember="clearing_agent_name" 
     comboBox1.DataBind(); 
     con.Close(); 
+0

감사합니다. 노력과 의견을 주셔서 감사합니다. 나는 시도하고 피드백을 드리겠습니다. – RichieCr7

+0

ur 코드가 다시 노력을 기울였습니다. 좀 더 정확하고 정확한 steve의 솔루션을 선택해야만합니다. – RichieCr7

관련 문제