C#winform连接MYSQL数据库斑马打印机打印标签

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using WindowsFormsMySql;
using BarTender;
using Microsoft.Build.BuildEngine;
using System.Data.SqlClient;


namespace printf
{
    public partial class commonprintf : Form
    {


        private static BarTender.Application btApp = new BarTender.Application();
        private static BarTender.Format btFormat = new BarTender.Format();

        //----------------------------------------------------------数据库连接-----------------------------------------------------------------
        MySqlConnection connection;
        public commonprintf()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            


            
        }
        //---------------------------------------------连接---------------------------------------------------
        private void button2_Click(object sender, EventArgs e)
        {
            string connStr = "server=localhost;port=3306;user=root;password=20010612Syf@;database=mytest;";
            connection = new MySqlConnection(connStr);
            try
            {
                connection.Open();
                MessageBox.Show("连接成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;

            }
        }
        //----------------------------------------断开-----------------------------------------------------------------
        private void button3_Click(object sender, EventArgs e)
        {
            string connStr = "server=localhost;port=3306;user=root;password=20010612Syf@;database=mytest;";
            connection = new MySqlConnection(connStr);
            try
            {
                connection.Close();
                MessageBox.Show("断开");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;

            }
        }
      

     

        private void button2_Click_1(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
            if (f2.DialogResult == DialogResult.OK)
            {
                //this.textBox1.Text = f2.str;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.ShowDialog();
            if (f3.DialogResult == DialogResult.OK)
            {
                
            }
        }
    }    
    
}

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using WindowsFormsMySql;
using BarTender;
using Microsoft.Build.BuildEngine;
using System.Data.SqlClient;

namespace printf
{
    public partial class Form2 : Form
    {
        private static BarTender.Application btApp = new BarTender.Application();
        private static BarTender.Format btFormat = new BarTender.Format();

        //----------------------------------------------------------数据库连接-----------------------------------------------------------------
        MySqlConnection connection;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            dataShow();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string connStr = "server=localhost;port=3306;user=root;password=20010612Syf@;database=mytest;";
            connection = new MySqlConnection(connStr);
            try
            {
                connection.Open();
                MessageBox.Show("连接成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string connStr = "server=localhost;port=3306;user=root;password=20010612Syf@;database=mytest;";
            connection = new MySqlConnection(connStr);
            try
            {
                connection.Close();
                MessageBox.Show("断开");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;

            }
        }
        private void dataShow()
        {



            // 数据库连接
            DBhelper sqlhelper = new DBhelper();
            //创建DataSet对象
            // DataSet reader = sqlhelper.GetDataSet("select b.Productcode as 'Productcode' , b.Part as 'Part' ,b.Pm as 'Pm',b.Vollage as 'Vollage',b.Colour as 'Colour',b.SAP as  'SAP',b.Type as 'Type',b.Date as  'Date',b.Line as  'Line' from label b", "label");
            DataSet reader = sqlhelper.GetDataSet("select b.Class as 'Class' , b.Color as 'Color' ,b.Date as 'Date',b.Fab as 'Fab',b.Line as 'Line',b.Part as  'Part',b.Pm as 'Pm',b.Type as  'Type' from chejian b", "chejian");
            // 给显示控件赋值
            this.dataGridView1.DataSource = reader.Tables[0];
            // 外观自适应宽度
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//自适应列宽
            }
            //-------------------------------------------------连接-------------------------------------------------------------------------

        }

        private void button4_Click(object sender, EventArgs e)
        {
            // 数据库连接
            DBhelper sqlhelper = new DBhelper();

            // sql语句
            //string sql = " update label set  Productcode  ='" + textBox1.Text + "',Part='" + textBox2.Text + "',Pm='" + textBox3.Text + "',Vollage='" + textBox4.Text + "',Colour='" + textBox5.Text + "',SAP='" + textBox6.Text + "',Type='" + textBox7.Text + "',Date='" + textBox8.Text + "',Line='" + textBox9.Text + "'where Productcode  ='" + textBox1.Text + "'";
            string sql = " update chejian set  Part  ='" + textBox1.Text + "',Class='" + textBox2.Text + "',Color='" + textBox3.Text + "',Date='" + textBox4.Text + "',Fab='" + textBox5.Text + "',Line='" + textBox6.Text + "',Pm='" + textBox7.Text + "',Type='" + textBox8.Text + "'where Part  ='" + textBox1.Text + "'";

            try
            {
                //创建DataSet对象
                sqlhelper.GetSqlCom(sql);
                dataShow();
            }
            catch
            {
                MessageBox.Show("修改失败");
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // 获取选中行的某个数据
            int rowindex = e.RowIndex;
            try
            {
                textBox1.Text = dataGridView1.Rows[rowindex].Cells[0].Value.ToString();
                textBox2.Text = dataGridView1.Rows[rowindex].Cells[1].Value.ToString();
                textBox3.Text = dataGridView1.Rows[rowindex].Cells[2].Value.ToString();
                textBox4.Text = dataGridView1.Rows[rowindex].Cells[3].Value.ToString();
                textBox5.Text = dataGridView1.Rows[rowindex].Cells[4].Value.ToString();

                textBox6.Text = dataGridView1.Rows[rowindex].Cells[5].Value.ToString();
                textBox7.Text = dataGridView1.Rows[rowindex].Cells[6].Value.ToString();
                textBox8.Text = dataGridView1.Rows[rowindex].Cells[7].Value.ToString();
               // textBox9.Text = dataGridView1.Rows[rowindex].Cells[8].Value.ToString();
            }
            catch
            {
                MessageBox.Show("赋值异常");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 数据库连接
            DBhelper sqlhelper = new DBhelper();

            // sql语句
            // string sql = "INSERT INTO `label` VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "')";
            string sql = "INSERT INTO `chejian` VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')";

            try
            {
                //创建DataSet对象
                sqlhelper.GetSqlCom(sql);
                dataShow();
            }
            catch
            {
                MessageBox.Show("插入数据异常");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // 数据库连接
            DBhelper sqlhelper = new DBhelper();

            // sql语句
            //string sql = " delete from label where Productcode='" + textBox1.Text + "'";
            string sql = " delete from chejian where Part='" + textBox1.Text + "'";
            try
            {
                //创建DataSet对象
                sqlhelper.GetSqlCom(sql);
                dataShow();
            }
            catch
            {
                MessageBox.Show("删除失败");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            {
                try
                {


                    btFormat = btApp.Formats.Open(AppDomain.CurrentDomain.BaseDirectory + "车间.btw", false, "");
                    btFormat.SetNamedSubStringValue("Class", textBox1.Text);
                    btFormat.SetNamedSubStringValue("Color", textBox2.Text);
                    btFormat.SetNamedSubStringValue("Date", textBox3.Text);
                    btFormat.SetNamedSubStringValue("Fab", textBox4.Text);
                    btFormat.SetNamedSubStringValue("Line", textBox5.Text);

                    btFormat.SetNamedSubStringValue("Part", textBox6.Text);
                    btFormat.SetNamedSubStringValue("Pm", textBox7.Text);
                    btFormat.SetNamedSubStringValue("Type", textBox8.Text);
                    //btFormat.SetNamedSubStringValue("Line", textBox9.Text);


                    btFormat.PrintOut(true, false); //第二个false设置打印时是否跳出打印属性
                    btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);//退出是是否保存标签
                }
                catch
                {
                    MessageBox.Show("打印失败");
                }

            }
        }
        
        private void button7_Click(object sender, EventArgs e)
        {
          
        
            

            this.DialogResult = DialogResult.OK;
        
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using WindowsFormsMySql;
using BarTender;
using Microsoft.Build.BuildEngine;
using System.Data.SqlClient;


namespace printf
{
    public partial class Form3 : Form
    {
        private static BarTender.Application btApp = new BarTender.Application();
        private static BarTender.Format btFormat = new BarTender.Format();

        //----------------------------------------------------------数据库连接-----------------------------------------------------------------
        MySqlConnection connection;
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            dataShow();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string connStr = "server=localhost;port=3306;user=root;password=20010612Syf@;database=mytest;";
            connection = new MySqlConnection(connStr);
            try
            {
                connection.Open();
                MessageBox.Show("连接成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string connStr = "server=localhost;port=3306;user=root;password=20010612Syf@;database=mytest;";
            connection = new MySqlConnection(connStr);
            try
            {
                connection.Close();
                MessageBox.Show("断开");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;

            }
        }
        private void dataShow()
        {



            // 数据库连接
            DBhelper sqlhelper = new DBhelper();
            //创建DataSet对象
            DataSet reader = sqlhelper.GetDataSet("select b.Productcode as 'Productcode' , b.Part as 'Part' ,b.Pm as 'Pm',b.Vollage as 'Vollage',b.Colour as 'Colour',b.SAP as  'SAP',b.Type as 'Type',b.Date as  'Date',b.Line as  'Line' from label b", "label");

            // 给显示控件赋值
            this.dataGridView1.DataSource = reader.Tables[0];
            // 外观自适应宽度
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//自适应列宽
            }
            //-------------------------------------------------连接-------------------------------------------------------------------------

        }

        private void button4_Click(object sender, EventArgs e)
        {
            // 数据库连接
            DBhelper sqlhelper = new DBhelper();

            // sql语句
            string sql = " update label set  Productcode  ='" + textBox1.Text + "',Part='" + textBox2.Text + "',Pm='" + textBox3.Text + "',Vollage='" + textBox4.Text + "',Colour='" + textBox5.Text + "',SAP='" + textBox6.Text + "',Type='" + textBox7.Text + "',Date='" + textBox8.Text + "',Line='" + textBox9.Text + "'where Productcode  ='" + textBox1.Text + "'";


            try
            {
                //创建DataSet对象
                sqlhelper.GetSqlCom(sql);
                dataShow();
            }
            catch
            {
                MessageBox.Show("修改失败");
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // 获取选中行的某个数据
            int rowindex = e.RowIndex;
            try
            {
                textBox1.Text = dataGridView1.Rows[rowindex].Cells[0].Value.ToString();
                textBox2.Text = dataGridView1.Rows[rowindex].Cells[1].Value.ToString();
                textBox3.Text = dataGridView1.Rows[rowindex].Cells[2].Value.ToString();
                textBox4.Text = dataGridView1.Rows[rowindex].Cells[3].Value.ToString();
                textBox5.Text = dataGridView1.Rows[rowindex].Cells[4].Value.ToString();

                textBox6.Text = dataGridView1.Rows[rowindex].Cells[5].Value.ToString();
                textBox7.Text = dataGridView1.Rows[rowindex].Cells[6].Value.ToString();
                textBox8.Text = dataGridView1.Rows[rowindex].Cells[7].Value.ToString();
                textBox9.Text = dataGridView1.Rows[rowindex].Cells[8].Value.ToString();
            }
            catch
            {
                MessageBox.Show("赋值异常");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 数据库连接
            DBhelper sqlhelper = new DBhelper();

            // sql语句
            string sql = "INSERT INTO `label` VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "')";


            try
            {
                //创建DataSet对象
                sqlhelper.GetSqlCom(sql);
                dataShow();
            }
            catch
            {
                MessageBox.Show("插入数据异常");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // 数据库连接
            DBhelper sqlhelper = new DBhelper();

            // sql语句
            string sql = " delete from label where Productcode='" + textBox1.Text + "'";

            try
            {
                //创建DataSet对象
                sqlhelper.GetSqlCom(sql);
                dataShow();
            }
            catch
            {
                MessageBox.Show("删除失败");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            {
                try
                {


                    btFormat = btApp.Formats.Open(AppDomain.CurrentDomain.BaseDirectory + "小包标签1.btw", false, "");
                    btFormat.SetNamedSubStringValue("Productcode", textBox1.Text);
                    btFormat.SetNamedSubStringValue("Part", textBox2.Text);
                    btFormat.SetNamedSubStringValue("Pm", textBox3.Text);
                    btFormat.SetNamedSubStringValue("Vollage", textBox4.Text);
                    btFormat.SetNamedSubStringValue("Colour", textBox5.Text);

                    btFormat.SetNamedSubStringValue("SAP", textBox6.Text);
                    btFormat.SetNamedSubStringValue("Type", textBox7.Text);
                    btFormat.SetNamedSubStringValue("Date", textBox8.Text);
                    btFormat.SetNamedSubStringValue("Line", textBox9.Text);


                    btFormat.PrintOut(true, false); //第二个false设置打印时是否跳出打印属性
                    btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);//退出是是否保存标签
                }
                catch
                {
                    MessageBox.Show("打印失败");
                }

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
         
       

         

            this.DialogResult = DialogResult.OK;

        
    }
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值