2016-10-12 47 views
0

打印標籤從數據庫中選擇的結果我已經先說這個,我是新來的windows編程,我如何使用循環

我有一個要求打印一組結果從搜索查詢得到的。每個結果行應打印在單個標籤上。查詢中包含樣本結果集。什麼是達到此目的的最佳方法。條形碼所示的欄應該打印爲條形碼,而另外兩欄應打印在標籤的上方和下方。 通常,我們可能必須在單擊一次按鈕時打印最多500個標籤。

下面我添加了我正在使用的代碼。在頁面加載事件中,我通過ID來加載數據庫中特定項目的數據。但是我需要使用選定的項目列表自動化,而不是一個。 Result set

public partial class PrintLabel : Form 
    { 
     string s_desc = ""; 
     string s_date = ""; 
     Image bCodeImage; 

     public PrintLabel() 
     { 
      InitializeComponent(); 
     } 
     private void PrintLabel_Load(object sender, EventArgs e) 
     { 
      FillData(735); 
     } 
     private void Print_Click(object sender, EventArgs e) 
     { 
      ItemPrint(); 
      this.Close(); 
     } 
     void FillData(int ID) 
     { 
      string str = Properties.Settings.Default.ConW; 
      using (SqlConnection conn = new SqlConnection(str)) 
      { 
       try 
       { 
        string query = "select item.ID+'-'+item.ItemLookupCode as Barcode,ExtendedDescription,GETDATE()+180 Expiry from Item where ID=" + ID + ";"; 
        conn.Open(); 
        SqlCommand cmd = new SqlCommand(query, conn); 
        SqlDataReader dtr = cmd.ExecuteReader(); 
        if (dtr.Read()) 
        { 
         GenerateBarcode(dtr[0].ToString()); 
         s_desc = dtr[1].ToString(); 
         s_date = dtr[2].ToString(); 
         lblDescription.Text = s_desc; 
         lblExpiry.Text = s_date; 
         PBBarcode.Image = bCodeImage; 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
     } 
     private void GenerateBarcode(string _bCodeImage) 
     { 
      string barCode = _bCodeImage; 
      Bitmap bitmap = new Bitmap(barCode.Length * 60, 750); 
      using (Graphics grapics = Graphics.FromImage(bitmap)) 
      { 
       Font ofont = new System.Drawing.Font("IDAHC39M Code 39 Barcode", 14); 
       PointF point = new PointF(2f, 2f); 
       SolidBrush black = new SolidBrush(Color.Black); 
       SolidBrush white = new SolidBrush(Color.White); 
       grapics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height); 
       grapics.DrawString("*" + barCode + "*", ofont, black, point); 
      } 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       bitmap.Save(ms, ImageFormat.Png); 
       bCodeImage = bitmap; 
      } 
     } 

     private void ItemPrint() 
     { 
      PrintDialog printdg = new PrintDialog(); 
      if (printdg.ShowDialog() == DialogResult.OK) 
      { 
       PrintDocument pd = new PrintDocument(); 
       pd.PrinterSettings = printdg.PrinterSettings; 
       pd.PrintPage += PrintPage; 
       pd.Print(); 
       pd.Dispose(); 
      } 
     } 
     private void PrintPage(object o, PrintPageEventArgs e) 
     { 
      lblDescription.Text = s_desc; 
      lblExpiry.Text = s_date; 
      PBBarcode.InitialImage = bCodeImage; 
     } 
    } 
+1

這裏有什麼問題?你使用什麼技術?它是桌面應用程序上的ASP.NET應用程序嗎?另外考慮閱讀[如何提問](http://stackoverflow.com/help/how-to-ask) –

+0

如果你需要在紙上打印,那麼你應該去一些報告SDK(水晶報告,報告銳利的射手等) – Michael

+0

你使用哪種語言?你有任何代碼嗎?你需要在屏幕或紙上打印? –

回答

2

我用它下面的代碼,張貼,因爲它可能幫助一些人在將來實現。

private void PrintLabelVal() 
     { 
      string str = Properties.Settings.Default.con; 
      SqlConnection Conn = new SqlConnection(str); 


      string query = " SELECT TempLotUpdate.Quantity,Item.BinLocation,Item.ItemLookupCode," + 
          " Item.ExtendedDescription,Item.LotNumber," + 
          " Item.ExpiryDate " + 
          " FROM TempLotUpdate" + 
          " INNER JOIN Item ON TempLotUpdate.ItemId = Item.ID" + 

          " WHERE TempLotUpdate.PONumber = '" + Globals.s_PON + "'; "; 
      int cnt = 0; 
      int var = 0; 
      SqlCommand cmd = new SqlCommand(query, Conn); 
      try 
      { 
       Conn.Open(); 
       SqlDataAdapter da = new SqlDataAdapter(); 
       da.SelectCommand = cmd; 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 

       for (var i = 0; i < dt.Rows.Count; i++) 
       { 
        cnt = Convert.ToInt32(dt.Rows[i][0]); 
        var = Convert.ToInt32(dt.Rows[i][4]); 
        for (var j = 0; j < cnt; j++) 
        { 
         var = Convert.ToInt32(dt.Rows[i][4]); 
         print(var); // Passing Lot ID 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

     private void print(int obj) 
     { 
      FillData(obj); 
      ItemPrint(); 
      this.Close(); 
     } 
     void FillData(int ID) 
     { 
      string str = Properties.Settings.Default.con; 
      using (SqlConnection conn = new SqlConnection(str)) 
      { 
       try 
       { 
        string query = "SELECT ItemLookupCode BarCode,Description,ExpiryDate,BinLocation,Price " + 
            " FROM Item where ID = " + ID + ";"; 
        conn.Open(); 
        SqlCommand cmd = new SqlCommand(query, conn); 
        SqlDataReader dtr = cmd.ExecuteReader(); 
        if (dtr.Read()) 
        { 

         barCode = dtr[0].ToString(); 
         s_desc = dtr[1].ToString(); 
         s_date = dtr[2].ToString(); 
         s_BinLoc = dtr[3].ToString(); 
         s_Price = dtr[4].ToString(); 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
     } 

     private void ItemPrint() 
     { 
      PrintDialog printdg = new PrintDialog(); 
      if (printdg.ShowDialog() == DialogResult.OK) 
      { 
       PrintDocument pd = new PrintDocument(); 
       pd.PrinterSettings = printdg.PrinterSettings; 
       pd.PrintPage += PrintPage; 
       pd.Print(); 
       pd.Dispose(); 
      } 
     } 
     private void PrintPage(object o, PrintPageEventArgs e) 
     { 
      string _desc = s_desc; 
      var _g = e.Graphics; 
      _g.DrawString(_desc, new Font("Arial Black", 7), Brushes.Black, 5, 8); 

      string _bCode = "*"+barCode+"*"; 
      var _f = e.Graphics; 
      _f.DrawString(_bCode, new Font("IDAHC39M Code 39 Barcode", 8), Brushes.Black, 4, 25); 

      string _BinLoc = s_BinLoc; 
      var _i = e.Graphics; 
      _i.DrawString(_BinLoc, new Font("Arial Narrow", 7), Brushes.Black, 5, 75); 

      string _date = "Exp: " + s_date.Substring(0, 10); 
      var _h = e.Graphics;   
      _h.DrawString(_date, new Font("Arial Narrow", 7), Brushes.Black, 45, 75); 

      string _Price = s_Price; 
      var _j = e.Graphics;   
      _j.DrawString(_Price, new Font("Arial Narrow", 7), Brushes.Black, 120, 75); 
     }