第四次作业 MySQL数据库及C#操作MySQL数据库 目标2
2015-05-20 16:21
423 查看
1.作业要求
目标2:C#操作MySQL数据库,包括基本步骤和具体的编码实现(90分)。
2.代码部分
tosql类:
form1类:
Update类:
3.程序截图
目标2:C#操作MySQL数据库,包括基本步骤和具体的编码实现(90分)。
2.代码部分
tosql类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; using System.Data; namespace WindowsFormsApplication3 { class tosql { public MySqlConnection getmysqlcon() { string M_str_sqlcon = "database=stu;Password=140222;User ID=root;"; // MySqlConnection myCon = new MySqlConnection(M_str_sqlcon); return myCon; } public void getmysqlcom(string M_str_sqlstr) { MySqlConnection mysqlcon = this.getmysqlcon(); mysqlcon.Open(); MySqlCommand commn = new MySqlCommand("set names gb2312", mysqlcon); MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon); mysqlcom.ExecuteNonQuery(); mysqlcom.Dispose(); mysqlcon.Close(); mysqlcon.Dispose(); } public DataTable getsqlread(string M_str_sqlstr) { MySqlConnection mysqlcon = this.getmysqlcon(); mysqlcon.Open(); MySqlCommand commn = new MySqlCommand("set names gb2312", mysqlcon); MySqlDataAdapter mda = new MySqlDataAdapter(M_str_sqlstr, mysqlcon); DataTable dt = new DataTable(); mda.Fill(dt); return dt; } } }
form1类:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.IO; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } tosql tosql_1 = new tosql(); public DataSet ExcelToDS(string path) { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + @path + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel = "select * from [sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); DataTable table1 = new DataTable(); ds = new DataSet(); myCommand.Fill(table1); myCommand.Fill(ds); return ds; } private void button1_Click(object sender, EventArgs e) { InsertExcel(); } private void InsertExcel() { OpenFileDialog openfile = new OpenFileDialog(); openfile.Filter = "工作薄(*.xls)|*.xls|所有文件(*.*)|*.*|工作薄(*.xlsx)|*.xlsx"; if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK) { DataSet dataSet = ExcelToDS(openfile.FileName); String sqlSearch = "select * from stu1"; int count = 0; string[] str = new string[4]; string sql; foreach (DataTable table in dataSet.Tables) { foreach (DataRow row in table.Rows) { count = 0; for (int i = 0; i < 4; i++) { str[i] = "*"; } foreach (DataColumn column in table.Columns) { if (row[column] != null) str[count] = row[column].ToString(); count++; } sql = string.Format("insert into stu1 values('{0}','{1}','{2}','{3}')", str[0], str[1], str[2], str[3]); if (!str[0].Equals("*")) { tosql_1.getmysqlcom(sql); } } } Reflash(sqlSearch); } } private void button2_Click(object sender, EventArgs e) { InsertTxt(); } private void InsertTxt() { OpenFileDialog openfile = new OpenFileDialog(); openfile.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*|word文档(*.doc)|*.doc"; if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK) { String sqlSearch = "select * from stu2"; string[] str = new string[5]; string sql; using (StreamReader sr = new StreamReader(openfile.FileName, Encoding.Default, false)) { string line; while ((line = sr.ReadLine()) != null) { line.Trim(); str = line.Split(new char[] { ' ' }); sql = string.Format("insert into stu2 values('{0}','{1}','{2}','{3}','{4}')", str[0], str[1], str[2], str[3], str[4]); tosql_1.getmysqlcom(sql); } } Reflash(sqlSearch); } } private void Reflash(string sqlSearch) { DataTable dataTable = tosql_1.getsqlread(sqlSearch); dataGridView1.DataSource = dataTable; } private void button3_Click(object sender, EventArgs e) { String sqlSearch = "select * from stu2 where NO NOT In(select NO from stu1);"; Reflash(sqlSearch); } private void button4_Click(object sender, EventArgs e) { int line = dataGridView1.SelectedRows.Count; if (line == 1) { Update update = new Update(); update.Show(); string[] strs = new string[4]; for (int i = 0; i < 4; i++) { strs[i] = dataGridView1.CurrentRow.Cells[i].Value.ToString(); } update.addText(strs[0], strs[1], strs[2], strs[3]); } else { MessageBox.Show("请选择需要修改那行的学号", "系统提示"); } } private void button5_Click(object sender, EventArgs e) { String sqlSearch = "select * from stu1"; Reflash(sqlSearch); } } }
Update类:
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; namespace WindowsFormsApplication3 { public partial class Update : Form { private TextBox textBox1; private Label label1; private Label label2; private TextBox textBox2; private Button button1; private Button button2; private Label label3; private Label label4; private TextBox textBox3; private TextBox textBox4; tosql connector = new tosql(); public Update() { InitializeComponent(); } public void addText(string strNo, string strName, string i, string grade) { textBox1.Text = strNo; textBox2.Text = strName; textBox3.Text = i; textBox4.Text = grade; textBox1.ReadOnly = true; } private void button1_Click(object sender, EventArgs e) { this.Visible = false; } private void button2_Click(object sender, EventArgs e) { string strNo = textBox1.Text.ToString(); string strName = textBox2.Text.ToString(); int classs = Convert.ToInt32(textBox3.Text.ToString()); string grades = textBox4.Text.ToString(); string sql = "update stu1 SET name='" + strName + "'," + "class=" + classs + "," + "website='" + grades + "' where NO='" + strNo + "'"; connector.getmysqlcom(sql); this.Visible = false; } private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(132, 26); this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(100, 21); this.textBox1.TabIndex = 0; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(61, 35); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(29, 12); this.label1.TabIndex = 1; this.label1.Text = "学号"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(61, 74); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(29, 12); this.label2.TabIndex = 2; this.label2.Text = "姓名"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(132, 65); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 21); this.textBox2.TabIndex = 3; // // button1 // this.button1.Location = new System.Drawing.Point(52, 213); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 4; this.button1.Text = "取消"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(167, 213); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 5; this.button2.Text = "确定"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(61, 121); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(29, 12); this.label3.TabIndex = 6; this.label3.Text = "组别"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(61, 158); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(29, 12); this.label4.TabIndex = 7; this.label4.Text = "网址"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(132, 112); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(100, 21); this.textBox3.TabIndex = 8; // // textBox4 // this.textBox4.Location = new System.Drawing.Point(132, 149); this.textBox4.Name = "textBox4"; this.textBox4.Size = new System.Drawing.Size(100, 21); this.textBox4.TabIndex = 9; // // Update // this.ClientSize = new System.Drawing.Size(284, 262); this.Controls.Add(this.textBox4); this.Controls.Add(this.textBox3); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.Name = "Update"; this.ResumeLayout(false); this.PerformLayout(); } } }
3.程序截图
相关文章推荐
- 【c#第四次作业】c#操作MySQL数据库
- 第四次作业:MySQL数据库及C#操作MySQL数据库
- 《C#程序设计》第四次作业:MySQL数据库及C#操作MySQL数据库
- C#第四次作业:MySQL数据库及C#操作MySQL数据库
- c#第四次作业 c#操作MySQL数据库
- C#第四次作业:MySQL数据库及C#操作MySQL数据库
- C#第四次作业:MYSQL数据库及C#操作MYSQL数据库——Winform程序
- 第四次C#作业-MySQL数据库及C#操作MySQL数据库
- 【c#第四次作业】c#操作SQLServer数据库
- 作业四:MYSQL数据库及C#操作MYSQL数据库——Winform程序
- C#第四次作业 数据库操作
- sql server数据库及c#操作sql server数据库(第四次C#作业)
- Java 第二次作业:MySQL数据库及Java操作MySQL数据库
- 《Java程序设计》第二次作业:MySQL数据库及Java操作MySQL数据库
- 第二个作业:MySQL数据库及Java操作MySQL数据库
- C#操作mysql数据库的实例
- 关于C#操作mysql数据库乱码
- C#操作MySql数据库帮助类(Dapper,T-Sql)
- 《Java程序设计》第二次作业:MySQL数据库及Java操作MySQL数据库
- c#第二次作业--随机图片(目标三)