2013-07-31 3 views
0

Windows 응용 프로그램 양식을 만들었습니다. 현재 당면한 문제가 있습니다. 문제는 : 이미 텍스트 상자 배열이 있었고 연결하려는 데이터가 int 데이터 형식이기 때문에 데이터베이스 텍스트 상자를 연결하려고했지만 데이터를 가져올 수 없습니다.int 데이터 형식을 문자열 데이터 형식으로 변환 C# 데이터베이스 액세스

내가 만든 2 개의 양식이 있는데, 하나는 로그인 양식, 로그인 양식, 데이터베이스는 문자열이므로 자동 완성 기능이 작동합니다. 두 번째 양식은 두 번째 양식이며 두 번째 양식의 경우 데이터베이스가 int이므로 자동 완성 기능이 작동하지 않습니다.

내 질문 : 어떻게 int 데이터 형식을 문자열 데이터 형식으로 변환합니까?

string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;"); 

    private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>(); 

    OleDbDataReader dReader; 
    OleDbConnection conn = new OleDbConnection(connectionString); 
    conn.Open(); 
    OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT [Code] FROM [Data] ORDER BY [Code] ASC", conn); 
    dReader = cmd.ExecuteReader(); 
    AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection(); 

    while (dReader.Read()) 
    { 
    codesCollection.Add(Convert.ToString(dReader.GetInt32(dReader.GetOrdinal("Code")))); // This line is not working 
    } 

//****TextBox for Code**** 
      for (int y = 0; y <= 16; y++) 
      { 
       textBoxCodeContainer.Add(new List<TextBox>()); 
       textBoxCodeContainer[0].Add(new TextBox()); 
       textBoxCodeContainer[0][y].Size = new Size(100, 50); 
       textBoxCodeContainer[0][y].Location = new Point(25, 150 + (y * 25)); 

       textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest; 
       textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource; 
       textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection; 

       dReader.Close(); 
       conn.Close(); 

       Controls.Add(textBoxCodeContainer[0][y]); 
      } 

편집 : 필드의 크기는 긴 정수 번호와 형식으로 대체 뒤에 .. 제로 0000 코드에 대한 데이터 유형이 숫자 "1"여기

는 두 번째 양식에 대한 내 코드입니다 , "2"등.

편집 : *전체 코드 ****

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Security.Principal; 
using System.Data.OleDb; 

namespace Sell_System 
{ 
    public partial class Form2 : Form 
    { 
     string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;"); 
     private Form1 firstForm; 
     private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>(); 
     private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>(); 
     private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>(); 
     private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>(); 
     private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>(); 

     public Form2() 
     { 
      InitializeComponent(); 
     } 

     public Form2(Form1 firstForm) 
      : this() 
     { 
      this.firstForm = firstForm; 
     } 

     private void Form2_Load(object sender, EventArgs e) 
     { 
      //var int num = 10; 

      OleDbDataReader dReader; 
      OleDbConnection conn = new OleDbConnection(connectionString); 
      conn.Open(); 
      OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT [Code] FROM [Data] ORDER BY [Code] ASC", conn); 
      dReader = cmd.ExecuteReader(); 
      AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection(); 

      while (dReader.Read()) 
      { 
       codesCollection.Add(Convert.ToString(dReader.GetInt32(dReader.GetOrdinal("Code")))); 
       //codesCollection.Add(Convert.ToString(num); 
      } 

      if (firstForm.comboBox1.SelectedIndex == 0) 
      { 
       label1.Text = "Code:"; 
       label1.Location = new Point(60, 125); 
       label2.Text = "Welcome to the Selling System."; 
       label2.Location = new Point(600, 0); 
       label3.Text = "Quantity:"; 
       label3.Location = new Point(155, 125); 
       label4.Text = "Description:"; 
       label4.Location = new Point(580, 125); 
       label5.Text = "Sub Total on Rp:"; 
       label5.Location = new Point(1020, 125); 
       label6.Text = "Total on Rp:"; 
       label6.Location = new Point(1210, 125); 
       label7.Text = "Total on Rp:"; 
       label7.Location = new Point(1080, 580); 
      } 

      else if (firstForm.comboBox1.SelectedIndex == 1) 
      { 
       label1.Text = "Kode:"; 
       label1.Location = new Point(60, 125); 
       label2.Text = "Selamat datang di Selling System."; 
       label2.Location = new Point(600, 0); 
       label3.Text = "Banyaknya:"; 
       label3.Location = new Point(145, 125); 
       label4.Text = "Keterangan:"; 
       label4.Location = new Point(580, 125); 
       label5.Text = "Sub Total di Rp:"; 
       label5.Location = new Point(1020, 125); 
       label6.Text = "Total di Rp:"; 
       label6.Location = new Point(1210, 125); 
       label7.Text = "Total di Rp:"; 
       label7.Location = new Point(1080, 580); 
      } 

      //****TextBox for Code**** 
      for (int y = 0; y <= 16; y++) 
      { 
       textBoxCodeContainer.Add(new List<TextBox>()); 
       textBoxCodeContainer[0].Add(new TextBox()); 
       textBoxCodeContainer[0][y].Size = new Size(100, 50); 
       textBoxCodeContainer[0][y].Location = new Point(25, 150 + (y * 25)); 

       textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest; 
       textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource; 
       textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection; 

       dReader.Close(); 
       conn.Close(); 

       Controls.Add(textBoxCodeContainer[0][y]); 
      } 

      //****TextBox for Quantity**** 
      for (int y = 0; y <= 16; y++) 
      { 
       textBoxQuantityContainer.Add(new List<TextBox>()); 
       textBoxQuantityContainer[0].Add(new TextBox()); 
       textBoxQuantityContainer[0][y].Size = new Size(100, 50); 
       textBoxQuantityContainer[0][y].Location = new Point(125, 150 + (y * 25)); 

       Controls.Add(textBoxQuantityContainer[0][y]); 
      } 

      //****TextBox for Description**** 
      for (int y = 0; y <= 16; y++) 
      { 
       textBoxDescContainer.Add(new List<TextBox>()); 
       textBoxDescContainer[0].Add(new TextBox()); 
       textBoxDescContainer[0][y].Size = new Size(750, 50); 
       textBoxDescContainer[0][y].Location = new Point(225, 150 + (y * 25)); 

       Controls.Add(textBoxDescContainer[0][y]); 
      } 

      //****TextBox for Sub Total**** 
      for (int y = 0; y <= 16; y++) 
      { 
       textBoxSubTotalContainer.Add(new List<TextBox>()); 
       textBoxSubTotalContainer[0].Add(new TextBox()); 
       textBoxSubTotalContainer[0][y].Size = new Size(175, 50); 
       textBoxSubTotalContainer[0][y].Location = new Point(975, 150 + (y * 25)); 

       Controls.Add(textBoxSubTotalContainer[0][y]); 
      } 

      //****TextBox for Total**** 
      for (int y = 0; y <= 16; y++) 
      { 
       textBoxTotalContainer.Add(new List<TextBox>()); 
       textBoxTotalContainer[0].Add(new TextBox()); 
       textBoxTotalContainer[0][y].Size = new Size(175, 50); 
       textBoxTotalContainer[0][y].Location = new Point(1150, 150 + (y * 25)); 

       Controls.Add(textBoxTotalContainer[0][y]); 
      } 

      //****TextBox for Total All**** 
      TextBox textBoxAllTotal = new TextBox(); 
      textBoxAllTotal.Size = new Size(175, 50); 
      textBoxAllTotal.Location = new Point(1150, 575); 

      Controls.Add(textBoxAllTotal); 
     } 
    } 
} 
+0

myInt.ToString() –

+0

여기에 질문을 게시하기 전에 google에 신경 쓰지 않아도 1 초가 걸릴 것입니다 ... – Derek

+0

그 줄은'codesCollection. Add (Convert.ToString (dReader.GetInt32 (dReader.GetOrdinal ("Code")));)가 작동하지 않습니다. 여기서 작동하지 않는 것은 무엇입니까? (코드 필드가 정수이고 Null이 아니라고 가정하면) – Steve

답변

1

내 질문은 : 어떻게 문자열 데이터 형 INT의 데이터 유형을 변환합니까? 사용

dReader.GetString("Code"); 

string s = Convert.ToString(yourIntvalue); 
+0

나는 그 코드를 이미 시험해 보았고, 작동하지 않고, 내 값은 데이터베이스에있다. 어떻게 그 값을 채울 수 있는가? (yourintvalue) – Reinhardt

+1

왜 'yourIntValue.ToString()'이 아닌가? – Tyler

+0

codesCollection.Add (Convert.ToString (dReader.GetOrdinal ("Code"))); – Ehsan

0

그래서

while (dReader.Read()) 
{ 
    codesCollection.Add(dReader.GetString("Code")); 
} 

OleDbDataReader.GetString

문자열로 지정된 열의 값을 취득.

+0

그것은 내게 몇 가지 오류가있다 : "문자열 형식을 int 형식으로 변환 할 수 없습니다"및 "dbdatareader.getstring (int)에 대한 최상의 오버로드 된 메서드가 잘못된 인수가 있습니다" – Reinhardt

0

모든 코드에서 CODE를 문자열로 사용하려는 경우 쿼리에서 변경하지 않으시겠습니까?

가에서는 CStr 트릭을 할 것입니다, 그래서

ACCESS 일부 VBScript를 지원합니다 "[코드] ASC BY [데이터] ORDER 구별에서는 CStr를 ([코드])을 선택합니다." 이 방법으로 Donig하면 DBReader는 CODE를 문자열로 처리합니다.

관련 문제