目标:
数据库用mysql,将数据苦衷的数据读到datagridview中,实现了词句的录入,修改,删除,查询。不能中英文混和输入,一个textbox中只能输入一种语言。不能重复录入等功能。望大家指正。
设计:
界面设计:
数据库设计:
代码:
public partial class DIC : Form
{
bool havesql = false;
String connetStr = "server=localhost;port=3306;user=登录名称;password=密码; database=数据库";
public DIC()
{
InitializeComponent();
dataGridView1.AllowUserToAddRows = false;
this.StartPosition = FormStartPosition.CenterScreen;
datafridviewupdat();
this.dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
/// <summary>
/// 更新数据
/// </summary>
private void datafridviewupdat()
{
string sql = "SELECT EnglinshName,CHinaName,Explanation FROM `dic`";
MySqlConnection conn = new MySqlConnection(connetStr);
conn.Open();
DataTable data = new DataTable();
MySqlDataAdapter reader1 = new MySqlDataAdapter(sql, conn);
reader1.Fill(data);
dataGridView1.DataSource = data;
conn.Close();
}
/// <summary>
/// 读
/// </summary>
/// <param name="sql"></param>
private void RSql(string sql)
{
MySqlConnection conn = new MySqlConnection(connetStr);
conn.Open();
MySqlCommand com = new MySqlCommand(sql, conn);
var dataa = com.ExecuteReader();
while (dataa.Read())
{
var f1 = dataa.GetValue(0);
var f2 = dataa.GetValue(1);
var f3 = dataa.GetValue(2);
textBox3.Text = f2.ToString();
textBox4.Text = f1.ToString();
textBox6.Text = f3.ToString();
}
if (dataa.Read() == false)
{
MessageBox.Show("该字节还未录入!");
}
conn.Close();
}
/// <summary>
/// 写/删
/// </summary>
/// <param name="sql"></param>
private void WSql(string sql)
{
MySqlConnection conn = new MySqlConnection(connetStr);
conn.Open();
MySqlCommand com = new MySqlCommand(sql, conn);
com.ExecuteNonQuery();
conn.Close();
}
/// <summary>
/// 验证该词语是否已存在
/// </summary>
/// <param name="sql"></param>
private bool HaveSql(string sql)
{
MySqlConnection conn = new MySqlConnection(connetStr);
conn.Open();
MySqlCommand com = new MySqlCommand(sql, conn);
com.ExecuteNonQuery();
if (com.ExecuteScalar() != null)
{
conn.Close();
return havesql;
}
else
{
conn.Close();
return havesql = true;
}
}
/// <summary>
/// 录入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
SaveAsTxt saveAsTxt = new SaveAsTxt();
int item1 = 0, item2 = 0;
Match match1 = Regex.Match(textBox1.Text, @"[A-Za-z]+$");
Match match2 = Regex.Match(textBox2.Text, @"[A-Za-z]+$");
Match match3 = Regex.Match(textBox1.Text, @"[0-9]+$");
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
{
for (int i = 0; i < textBox1.Text.Length; i++)
{
if ((int)textBox1.Text[i] > 127)
{
item1++;
}
else
{
item2--;
}
}
if (item1 > 0 && item1 < 0)
{
MessageBox.Show("请输入一种语言");
}
//输入中文
if (item1 > 0 && item2 == 0)
{
if (match2.Success)
{
string sql = "SELECT Number FROM `diactionaris`" +
" where ChinaName ='" + textBox1.Text + "'and EnglinshName='"+ textBox2.Text + "'and Explanation='"+ textBox5.Text + "'";
if (HaveSql(sql) == true)
{
string sqlinsert = " insert into diactionaris(EnglinshName , CHinaName,Explanation)" +
" VALUES('" + textBox2.Text + "', '" + textBox1.Text + "','" + textBox5.Text + "')";
WSql(sqlinsert);
Thread thread = new Thread(new ThreadStart(a));
thread.Start();
MessageBox.Show("保存成功");
}
else
{
MessageBox.Show("该词条已存在");
}
}
}
//输入英文
if (item1 == 0 && item2 < 0)
{
if (match1.Success || match3.Success)
{
string sql = "SELECT Number FROM `dic`" +
" where EnglinshName ='" + textBox1.Text + "'and EnglinshName='" + textBox2.Text + "'and Explanation='" + textBox5.Text + "'";
if (HaveSql(sql) == true)
{
string sqlinsert = " insert into dic(EnglinshName , CHinaName,Explanation)" +
" VALUES('" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox5.Text + "')";
WSql(sqlinsert);
Thread thread = new Thread(new ThreadStart (a));
thread.Start();
MessageBox.Show("保存成功");
}
else
{
MessageBox.Show("该词条已存在");
}
}
}
}
else
{
MessageBox.Show("内容不能为空");
}
datafridviewupdat();
}
SaveAsTxt saveAsTxt = new SaveAsTxt();
private void a()
{
saveAsTxt.SaveTxt(textBox1.Text, textBox2.Text, textBox5.Text);
}
/// <summary>
/// 清空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox5.Clear();
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
int item1 = 0, item2 = 0;
if (!string.IsNullOrEmpty(textBox3.Text))
{
for (int i = 0; i < textBox3.Text.Length; i++)
{
if ((int)textBox3.Text[i] > 127)
{
item1++;
}
else
{
item2--;
}
}
//输入中文
if (item1 > 0 && item2 == 0)
{
string sql = "select ChinaName,EnglinshName,Explanation from dic where " +
" ChinaName like '%" + textBox3.Text + "' or ChinaName like '" + textBox3.Text + "%'";
RSql(sql);
}
if (item1 == 0 && item2 < 0)
{
string sql = "select ChinaName,EnglinshName,Explanation from dic where " +
" EnglinshName like '%" + textBox3.Text + "' or EnglinshName like '" + textBox3.Text + "%'";
RSql(sql);
}
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
int index = dataGridView1.CurrentRow.Index;
string[] str = new string[dataGridView1.Rows.Count];
str[index] = dataGridView1.Rows[index].Cells["EnglinshName"].Value.ToString();
string sql = "DELETE FROM `dic` where EnglinshName='" + str[index] + "'";
WSql(sql);
datafridviewupdat();
// Thread thread = new Thread(new ThreadStart(parameter));
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
int index = dataGridView1.CurrentRow.Index;
textBox1 .Text = dataGridView1.Rows[index].Cells["EnglinshName"].Value.ToString();
textBox2.Text = dataGridView1.Rows[index].Cells["ChinaName"].Value.ToString();
textBox5.Text = dataGridView1.Rows[index].Cells["Explanation"].Value.ToString();
}
/// <summary>
///添加序号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
int index = dataGridView1.RowCount;
e.Row.HeaderCell.Value = string.Format("{0}",e.Row.Index + 1);
// this.dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}