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)
{
}
}
}
}
文章来源:https://www.toymoban.com/news/detail-512543.html
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;
}
}
}
文章来源地址https://www.toymoban.com/news/detail-512543.html
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;
}
}
}
到了这里,关于C#winform连接MYSQL数据库斑马打印机打印标签的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!