-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddcom.xaml.cs
129 lines (128 loc) · 5.14 KB
/
Addcom.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;
namespace MQTT
{
/// <summary>
/// Addcom.xaml 的互動邏輯
/// </summary>
public partial class Addcom : Window
{
private MainWindow mainWindow;
public Addcom(MainWindow mainWindow)
{
InitializeComponent();
this.mainWindow = mainWindow;
this.WindowState = WindowState.Maximized;
}
List<string> comID = new List<string>();
private void Button_Click(object sender, RoutedEventArgs e)
{
string database = "company_db";
string databaseServer = "220.132.141.9";
string databasePort = "6833";
string databaseUser = "root";
string databasePassword = "edys1234";
string connectionString = $"server={databaseServer};" + $"port={databasePort};" + $"user={databaseUser};" + $"password={databasePassword};" + $"database={database};" + "charset=utf8;";
if (txtid.Text.Length > 0 && txtname.Text.Length > 0 && txtphone.Text.Length > 0)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open(); //資料庫連線my'Unable to connect to any of the specified MySQL hosts.''Unable to connect to any of the specified MySQL hosts.'
// 在這裡執行資料庫操作
string sql = "SELECT * FROM company_info_db";
using (MySqlCommand cmd = new MySqlCommand(sql, connection))
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
comID.Add(reader.GetString("Company_ID"));
}
}
}
connection.Close();
}
var foundItems = comID.Where(item => item.Contains(txtid.Text)).ToList();
if (foundItems.Any())
{
MessageBox.Show("部門ID不能重複");
return;
}
}
else
{
MessageBox.Show("資料不能有空");
return;
}
MessageBoxResult result = MessageBox.Show("確定要上傳嗎?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes){
string buff="";
for (int i = 0; i < txtphone.Text.Length; i++)
{
if (char.IsDigit(txtphone.Text[i]))
{
buff += txtphone.Text[i];
}
}
if (buff[1].ToString()=="9")
{
if(buff.Length != 10) //0932690245
{
MessageBox.Show("行動電話有誤或不完整");
return;
}
}
else
{
if (buff.Length != 9) //055875935
{
MessageBox.Show("室內電話有誤或不完整");
return;
}
}
txtphone.Text = buff;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string insertSql =
"INSERT INTO company_info_db (Company_ID, Name, Address, Cellphone) VALUES (@id, @name, @address, @cellphone)";
using (MySqlCommand insertCommand = new MySqlCommand(insertSql, connection))
{
insertCommand.Parameters.AddWithValue("@id", txtid.Text);
insertCommand.Parameters.AddWithValue("@name", txtname.Text);
insertCommand.Parameters.AddWithValue("@address",txtaddress.Text);
insertCommand.Parameters.AddWithValue("@cellphone", txtphone.Text);
int rowsAffected = insertCommand.ExecuteNonQuery();
}
connection.Close();
}
mainWindow.MySQLCreatelist();
this.Close();
}else{
return;
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MessageBoxResult result = MessageBox.Show("確定要放棄嗎?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes){
this.Close();
}else{
return;
}
}
}
}