2009-07-20 50 views
1

我的另一個SQL問題..SQL Server CE閱讀器的問題,它不想閱讀!

這個時候,讀者無法正常工作(我是這麼認爲的) 我用這個代碼,我得到的只有1記錄添加和我的數據庫有記錄100`s ..

public void addPosts() 
    { 

     string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\msgdb.sdf"; 

     string sql; 
     PictureBox avaBox = new PictureBox(); 
     PictureBox pictureBox1 = new PictureBox(); 
     Button atBtn = new Button(); 
     RichTextBox msgBox = new RichTextBox(); 
     Panel panelz = new Panel(); 
     DateTime dt = DateTime.Now; 
     SqlCeConnection connection = new SqlCeConnection("datasource=" + dbfile); 
     // Read all rows from the table test_table into a dataset (note, the adapter automatically opens the connection) 
     SqlCeDataAdapter adapter = new SqlCeDataAdapter("select * from posts", connection); 
     DataSet data = new DataSet(); 
     adapter.Fill(data); 

     SqlCeCommand cmd = new SqlCeCommand(); 
     cmd.Connection = connection; 
     sql = "Select user_from,msg,avatar FROM posts"; 
     cmd.CommandText = sql; 
     connection.Open(); 
     SqlCeDataReader reader = cmd.ExecuteReader(); 
     int i = 0; 
     while (reader.Read()) 
      { 
       i++; 
       string ava = reader.GetString(2); 
       string usrFrom = reader.GetString(0); 
       string messige = reader.GetString(1); 
       // 
       // groupBox1 
       // 
       panelz.Controls.Add(pictureBox1); 
       panelz.Controls.Add(atBtn); 
       panelz.Controls.Add(avaBox); 
       panelz.Controls.Add(msgBox); 
       panelz.Location = new System.Drawing.Point(0, 335); 
       panelz.Name = "panel"+ i; 
       panelz.Size = new System.Drawing.Size(335, 90); 

       panelz.TabIndex = 0; 
       panelz.TabStop = false; 


       // 
       // pictureBox1 
       // 
       pictureBox1.Dock = System.Windows.Forms.DockStyle.Right; 
       pictureBox1.Image = global::m23.Properties.Resources.post_area; 
       pictureBox1.Location = new System.Drawing.Point(58, 0); 
       pictureBox1.Name = "pictureBox1"+i; 
       pictureBox1.Size = new System.Drawing.Size(281, 99); 
       pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; 
       pictureBox1.TabIndex = 1; 
       pictureBox1.TabStop = false; 
       // 
       // atBtn 
       // 
       atBtn.AutoSize = true; 
       atBtn.BackgroundImage = global::m23.Properties.Resources.post_user; 
       atBtn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; 
       atBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 
       atBtn.Location = new System.Drawing.Point(0, 62); 
       atBtn.Name = "atBtn"+i; 
       atBtn.Size = new System.Drawing.Size(28, 25); 
       atBtn.TabIndex = 2; 
       atBtn.UseVisualStyleBackColor = true; 

       // 
       avaBox.Location = new System.Drawing.Point(0, 0); 
       avaBox.Name = "avaBox"+i; 
       avaBox.Size = new System.Drawing.Size(53, 53); 
       avaBox.TabIndex = 4; 
       avaBox.TabStop = false; 
       avaBox.ImageLocation = "http://img.edno23.com/avatars/thumbs/" + ava; 

       // 
       msgBox.BorderStyle = System.Windows.Forms.BorderStyle.None; 
       msgBox.Location = new System.Drawing.Point(76, 10); 

       msgBox.Name = "msgBox"+i; 
       msgBox.ReadOnly = true; 
       msgBox.BackColor = Color.White; 
       msgBox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None; 
       msgBox.Size = new System.Drawing.Size(251, 68); 
       msgBox.TabIndex = 3; 
       msgBox.Text = messige; 
       msgBox.BringToFront(); 
       // 

       CommonFlowPanel.Controls.Add(panelz); 


      } 


     connection.Close(); 
    } 

感謝您的幫助!

回答

2

將面板的聲明和所有在每個面板上的控件放到while循環中。你基本上一次又一次地重新添加一次Panel(「panelz」)。你想要做的是在while循環中爲每一行創建一個新的面板,以及面板上每個控件的新實例。

+0

AHH !!有時它很簡單..我覺得我太累了,現在就寫代碼吧:)謝謝你的回答! – Aviatrix 2009-07-20 22:39:12