//删除数据 private void button4_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox1.Text == null) { MessageBox.Show(this, "桥梁编号不允许为空!", "删除失败"); return; } MySqlConnection mysql = CsharpMySQL.GetMySqlConnection(); mysql.Open(); string cmd = "DELETE FROM BasicInfor WHERE bridgeID = '" + textBox1.Text + "';"; cmd += "DELETE FROM VertexInfor WHERE bridgeID = '" + textBox1.Text + "';"; MySqlCommand mySqlCommand = CsharpMySQL.GetSqlCommand(cmd, mysql); CsharpMySQL.GetDelete(mySqlCommand); mysql.Close(); }
private void Form1_Load(object sender, EventArgs e) { MySqlConnection mysql = new MySqlConnection(); try { //初始化mysql - C# 连接 mysql = CsharpMySQL.GetMySqlConnection(); mysql.Open(); //获取数据 string cmd = @" SELECT BasicInfor.*,VertexInfor.VertexID,VertexInfor.VertexName,VertexInfor.VertexLongitude,VertexInfor.VertexLatitude,VertexInfor.VertexAltitude FROM BasicInfor LEFT OUTER JOIN VertexInfor ON BasicInfor.bridgeID= VertexInfor.bridgeID ORDER BY BasicInfor.bridgeID;"; MySqlCommand mySqlCommand = CsharpMySQL.GetSqlCommand(cmd, mysql); MySqlDataAdapter adapter = new MySqlDataAdapter(mySqlCommand); DataSet ds = new DataSet(); adapter.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; /*dataGridView1.Columns[0].Width = 80; * dataGridView1.Columns[1].Width = 80; * dataGridView1.Columns[2].Width = 80; * dataGridView1.Columns[3].Width = 233; * dataGridView1.Columns[4].Width = 100; * dataGridView1.Columns[5].Width = 80; * dataGridView1.Columns[6].Width = 80; * dataGridView1.Columns[7].Width = 80; * dataGridView1.Columns[8].Width = 80;*/ dataGridView1.Columns[0].HeaderText = "桥梁编号"; dataGridView1.Columns[1].HeaderText = "桥梁名称"; dataGridView1.Columns[2].HeaderText = "桥梁类型"; dataGridView1.Columns[3].HeaderText = "桥梁简介"; dataGridView1.Columns[4].HeaderText = "桥梁跨度"; dataGridView1.Columns[5].HeaderText = "顶点编号"; dataGridView1.Columns[6].HeaderText = "顶点名称"; dataGridView1.Columns[7].HeaderText = "顶点经度"; dataGridView1.Columns[8].HeaderText = "顶点纬度"; dataGridView1.Columns[9].HeaderText = "顶点海拔"; dataGridView1.ReadOnly = true; cmd = @" SELECT PartsInfor.bridgeID , PartsInfor.PartID , PartsInfor.PartName , PartsInfor.PartType , PartsVertexInfor.PartsVertexID, PartsVertexInfor.PartsVertexName , PartsVertexInfor.PartLongitude , PartsVertexInfor.PartLatitude , PartsVertexInfor.PartAltitude FROM PartsInfor LEFT OUTER JOIN PartsVertexInfor ON PartsInfor.PartID = PartsVertexInfor.PartID AND PartsInfor.BridgeID = PartsVertexInfor.bridgeID ORDER BY PartsInfor.bridgeID;"; mySqlCommand = CsharpMySQL.GetSqlCommand(cmd, mysql); adapter = new MySqlDataAdapter(mySqlCommand); ds = new DataSet(); adapter.Fill(ds); dataGridView2.DataSource = ds.Tables[0]; } catch (Exception) { MessageBox.Show(this, "数据库连接失败!", "初始化失败"); mysql.Close(); } finally { //关闭连接 mysql.Close(); } }
//修改 private void button3_Click(object sender, EventArgs e) { MySqlConnection mysql = new MySqlConnection(); try { //初始化mysql - C# 连接 mysql = CsharpMySQL.GetMySqlConnection(); mysql.Open(); string cmd = "DELETE FROM PartsInfor WHERE bridgeID = '" + textBox1.Text + "'AND PartID = '" + textBox3.Text + "';"; cmd += "DELETE FROM PartsVertexInfor WHERE bridgeID = '" + textBox1.Text + "'AND PartID = '" + textBox3.Text + "';"; //获取数据 cmd += @"INSERT INTO `bridge`.`partsinfor` (`bridgeID`, `PartID`, `PartName`, `PartType`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox6.Text + "');"; if (textBox2.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox2.Text + "','" + textBox7.Text + "','" + float.Parse(textBox8.Text) + "','" + float.Parse(textBox9.Text) + "','" + float.Parse(textBox10.Text) + "');"; } if (textBox15.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox15.Text + "','" + textBox14.Text + "','" + float.Parse(textBox13.Text) + "','" + float.Parse(textBox12.Text) + "','" + float.Parse(textBox11.Text) + "');"; } if (textBox20.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox20.Text + "','" + textBox19.Text + "','" + float.Parse(textBox18.Text) + "','" + float.Parse(textBox17.Text) + "','" + float.Parse(textBox16.Text) + "');"; } if (textBox25.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox25.Text + "','" + textBox24.Text + "','" + float.Parse(textBox23.Text) + "','" + float.Parse(textBox22.Text) + "','" + float.Parse(textBox21.Text) + "');"; } if (textBox30.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox30.Text + "','" + textBox29.Text + "','" + float.Parse(textBox28.Text) + "','" + float.Parse(textBox27.Text) + "','" + float.Parse(textBox26.Text) + "');"; } if (textBox35.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox35.Text + "','" + textBox34.Text + "','" + float.Parse(textBox33.Text) + "','" + float.Parse(textBox32.Text) + "','" + float.Parse(textBox31.Text) + "');"; } if (textBox40.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox40.Text + "','" + textBox39.Text + "','" + float.Parse(textBox38.Text) + "','" + float.Parse(textBox37.Text) + "','" + float.Parse(textBox36.Text) + "');"; } if (textBox45.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox45.Text + "','" + textBox44.Text + "','" + float.Parse(textBox43.Text) + "','" + float.Parse(textBox42.Text) + "','" + float.Parse(textBox41.Text) + "');"; } Debug.WriteLine(cmd); MySqlCommand mySqlCommand = CsharpMySQL.GetSqlCommand(cmd, mysql); CsharpMySQL.GetUpdate(mySqlCommand); } catch { MessageBox.Show(this, "修改失败", "修改失败"); } finally { mysql.Close(); } }
//查询 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length == 0 || textBox3.Text.Length == 0) { textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; textBox9.Text = ""; textBox10.Text = ""; textBox11.Text = ""; textBox12.Text = ""; textBox13.Text = ""; textBox14.Text = ""; textBox15.Text = ""; textBox16.Text = ""; textBox17.Text = ""; textBox18.Text = ""; textBox19.Text = ""; textBox20.Text = ""; textBox21.Text = ""; textBox22.Text = ""; textBox23.Text = ""; textBox24.Text = ""; textBox25.Text = ""; textBox26.Text = ""; textBox27.Text = ""; textBox28.Text = ""; textBox29.Text = ""; textBox30.Text = ""; textBox31.Text = ""; textBox32.Text = ""; textBox33.Text = ""; textBox34.Text = ""; textBox35.Text = ""; textBox36.Text = ""; textBox37.Text = ""; textBox38.Text = ""; textBox39.Text = ""; textBox40.Text = ""; textBox41.Text = ""; textBox42.Text = ""; textBox43.Text = ""; textBox44.Text = ""; textBox45.Text = ""; MessageBox.Show(this, "编号不允许为空!", "查询失败"); } else { string cmd = @" SELECT PartsInfor.* , PartsVertexInfor.* FROM PartsInfor, PartsVertexInfor WHERE PartsInfor.bridgeID = PartsVertexInfor.bridgeID AND PartsInfor.PartID = PartsVertexInfor.PartID AND PartsVertexInfor.bridgeID = '"; cmd = cmd + textBox1.Text + "'AND PartsVertexInfor.PartID = '" + textBox3.Text + "';"; Debug.WriteLine(cmd); //初始化mysql - C# 连接 MySqlConnection mysql = CsharpMySQL.GetMySqlConnection(); mysql.Open(); MySqlCommand mySqlCommand = CsharpMySQL.GetSqlCommand(cmd, mysql); MySqlDataReader reader = mySqlCommand.ExecuteReader(); int counter = 0; try { Boolean flag = reader.Read(); if (flag == false) { //没有顶点数据; string cmd1 = "SELECT * FROM PartsInfor WHERE PartsInfor.bridgeID = '" + textBox1.Text + "' AND PartsInfor.PartID = '" + textBox3.Text + "';"; Debug.WriteLine(cmd1); MySqlConnection mysql1 = CsharpMySQL.GetMySqlConnection(); mysql1.Open(); MySqlCommand mySqlCommand1 = CsharpMySQL.GetSqlCommand(cmd1, mysql1); MySqlDataReader reader1 = mySqlCommand1.ExecuteReader(); try { Boolean flag1 = reader1.Read(); if (flag1 == false) { textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; textBox9.Text = ""; textBox10.Text = ""; textBox11.Text = ""; textBox12.Text = ""; textBox13.Text = ""; textBox14.Text = ""; textBox15.Text = ""; textBox16.Text = ""; textBox17.Text = ""; textBox18.Text = ""; textBox19.Text = ""; textBox20.Text = ""; textBox21.Text = ""; textBox22.Text = ""; textBox23.Text = ""; textBox24.Text = ""; textBox25.Text = ""; textBox26.Text = ""; textBox27.Text = ""; textBox28.Text = ""; textBox29.Text = ""; textBox30.Text = ""; textBox31.Text = ""; textBox32.Text = ""; textBox33.Text = ""; textBox34.Text = ""; textBox35.Text = ""; textBox36.Text = ""; textBox37.Text = ""; textBox38.Text = ""; textBox39.Text = ""; textBox40.Text = ""; textBox41.Text = ""; textBox42.Text = ""; textBox43.Text = ""; textBox44.Text = ""; textBox45.Text = ""; MessageBox.Show(this, "没有记录!", "查询失败"); } while (flag1) { if (reader1.HasRows) { string[] textBoxShowStr = new string[4]; for (int i = 0; i < 4; i++) { if (reader1[i].ToString() == "" || reader1[i].ToString() == "NULL" || reader1[i].ToString() == null) { textBoxShowStr[i] = "暂无!"; } else { textBoxShowStr[i] = reader1.GetString(i); } } // textBox1.Text = textBoxShowStr[0]; // textBox3.Text = textBoxShowStr[1]; //textBox5.Text = textBoxShowStr[3]; textBox4.Text = textBoxShowStr[2]; textBox6.Text = textBoxShowStr[3]; textBox2.Text = ""; textBox7.Text = ""; textBox8.Text = ""; textBox9.Text = ""; textBox10.Text = ""; textBox11.Text = ""; textBox12.Text = ""; textBox13.Text = ""; textBox14.Text = ""; textBox15.Text = ""; textBox16.Text = ""; textBox17.Text = ""; textBox18.Text = ""; textBox19.Text = ""; textBox20.Text = ""; textBox21.Text = ""; textBox22.Text = ""; textBox23.Text = ""; textBox24.Text = ""; textBox25.Text = ""; textBox26.Text = ""; textBox27.Text = ""; textBox28.Text = ""; textBox29.Text = ""; textBox30.Text = ""; textBox31.Text = ""; textBox32.Text = ""; textBox33.Text = ""; textBox34.Text = ""; textBox35.Text = ""; textBox36.Text = ""; textBox37.Text = ""; textBox38.Text = ""; textBox39.Text = ""; textBox40.Text = ""; textBox41.Text = ""; textBox42.Text = ""; textBox43.Text = ""; textBox44.Text = ""; textBox45.Text = ""; } flag1 = reader1.Read(); } } catch { MessageBox.Show(this, "查询失败!", "查询失败"); } finally { reader1.Close(); mysql1.Close(); } } while (flag) { string[] textBoxShowStr = new string[11]; if (reader.HasRows) { for (int i = 0; i < 11; i++) { if (reader[i].ToString() == "" || reader[i].ToString() == "NULL" || reader[i].ToString() == null) { textBoxShowStr[i] = "暂无!"; } else { textBoxShowStr[i] = reader.GetString(i); } } switch (counter) { case 0: { textBox2.Text = textBoxShowStr[6]; textBox7.Text = textBoxShowStr[7]; textBox8.Text = textBoxShowStr[8]; textBox9.Text = textBoxShowStr[9]; textBox10.Text = textBoxShowStr[10]; break; } case 1: { textBox15.Text = textBoxShowStr[6]; textBox14.Text = textBoxShowStr[7]; textBox13.Text = textBoxShowStr[8]; textBox12.Text = textBoxShowStr[9]; textBox11.Text = textBoxShowStr[10]; break; } case 2: { textBox20.Text = textBoxShowStr[6]; textBox19.Text = textBoxShowStr[7]; textBox18.Text = textBoxShowStr[8]; textBox17.Text = textBoxShowStr[9]; textBox16.Text = textBoxShowStr[10]; break; } case 3: { textBox25.Text = textBoxShowStr[6]; textBox24.Text = textBoxShowStr[7]; textBox23.Text = textBoxShowStr[8]; textBox22.Text = textBoxShowStr[9]; textBox21.Text = textBoxShowStr[10]; break; } case 4: { textBox30.Text = textBoxShowStr[6]; textBox29.Text = textBoxShowStr[7]; textBox28.Text = textBoxShowStr[8]; textBox27.Text = textBoxShowStr[9]; textBox26.Text = textBoxShowStr[10]; break; } case 5: { textBox35.Text = textBoxShowStr[6]; textBox34.Text = textBoxShowStr[7]; textBox33.Text = textBoxShowStr[8]; textBox32.Text = textBoxShowStr[9]; textBox31.Text = textBoxShowStr[10]; break; } case 6: { textBox40.Text = textBoxShowStr[6]; textBox39.Text = textBoxShowStr[7]; textBox38.Text = textBoxShowStr[8]; textBox37.Text = textBoxShowStr[9]; textBox36.Text = textBoxShowStr[10]; break; } case 7: { textBox45.Text = textBoxShowStr[6]; textBox44.Text = textBoxShowStr[7]; textBox43.Text = textBoxShowStr[8]; textBox43.Text = textBoxShowStr[9]; textBox42.Text = textBoxShowStr[10]; break; } } counter = counter + 1; // textBox1.Text = textBoxShowStr[0]; // textBox3.Text = textBoxShowStr[1]; //textBox5.Text = textBoxShowStr[3]; textBox4.Text = textBoxShowStr[2]; textBox6.Text = textBoxShowStr[3]; } flag = reader.Read(); } } catch (Exception) { MessageBox.Show(this, "查询失败!", "查询失败"); } finally { reader.Close(); mysql.Close(); counter = 0; } } }
//添加 private void button2_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox1.Text == null) { MessageBox.Show(this, "桥梁编号不允许为空!", "添加失败"); return; } if (textBox3.Text == "" || textBox3.Text == null) { MessageBox.Show(this, "部件编号不允许为空!", "添加失败"); return; } if (textBox4.Text == "" || textBox4.Text == null) { MessageBox.Show(this, "部件名称不允许为空!", "添加失败"); return; } if (textBox6.Text == "" || textBox4.Text == null) { MessageBox.Show(this, "部件类型不允许为空!", "添加失败"); return; } MySqlConnection mysql = new MySqlConnection(); try { //初始化mysql - C# 连接 mysql = CsharpMySQL.GetMySqlConnection(); mysql.Open(); //获取数据 string cmd = @"INSERT INTO `bridge`.`partsinfor` (`bridgeID`, `PartID`, `PartName`, `PartType`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox6.Text + "');"; if (textBox2.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox2.Text + "','" + textBox7.Text + "','" + float.Parse(textBox8.Text) + "','" + float.Parse(textBox9.Text) + "','" + float.Parse(textBox10.Text) + "');"; } if (textBox15.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox15.Text + "','" + textBox14.Text + "','" + float.Parse(textBox13.Text) + "','" + float.Parse(textBox12.Text) + "','" + float.Parse(textBox11.Text) + "');"; } if (textBox20.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox20.Text + "','" + textBox19.Text + "','" + float.Parse(textBox18.Text) + "','" + float.Parse(textBox17.Text) + "','" + float.Parse(textBox16.Text) + "');"; } if (textBox25.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox25.Text + "','" + textBox24.Text + "','" + float.Parse(textBox23.Text) + "','" + float.Parse(textBox22.Text) + "','" + float.Parse(textBox21.Text) + "');"; } if (textBox30.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox30.Text + "','" + textBox29.Text + "','" + float.Parse(textBox28.Text) + "','" + float.Parse(textBox27.Text) + "','" + float.Parse(textBox26.Text) + "');"; } if (textBox35.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox35.Text + "','" + textBox34.Text + "','" + float.Parse(textBox33.Text) + "','" + float.Parse(textBox32.Text) + "','" + float.Parse(textBox31.Text) + "');"; } if (textBox40.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox40.Text + "','" + textBox39.Text + "','" + float.Parse(textBox38.Text) + "','" + float.Parse(textBox37.Text) + "','" + float.Parse(textBox36.Text) + "');"; } if (textBox45.Text != "") { cmd += "INSERT INTO `bridge`.`partsvertexinfor` (`bridgeID`, `PartID`, `PartsVertexID`, `PartsVertexName`, `PartLongitude`, `PartLatitude`, `PartAltitude`) VALUES('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox45.Text + "','" + textBox44.Text + "','" + float.Parse(textBox43.Text) + "','" + float.Parse(textBox42.Text) + "','" + float.Parse(textBox41.Text) + "');"; } Debug.WriteLine(cmd); MySqlCommand mySqlCommand = CsharpMySQL.GetSqlCommand(cmd, mysql); CsharpMySQL.GetInsert(mySqlCommand); } catch (Exception) { MessageBox.Show(this, "添加失败", "添加失败"); } finally { mysql.Close(); } }