-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
62 lines (54 loc) · 1.94 KB
/
Form1.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
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SQLQueryApp:
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ExecuteQueryButton_Click(object sender, EventArgs e)
{
string serverIp = ServerIpTextBox.Text;
string username = UsernameTextBox.Text;
string password = PasswordTextBox.Text;
string query = QueryTextBox.Text;
string connectionString = $"Data Source={serverIp};Initial Catalog=HC;User ID={username};Password={password};";
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
// 清空結果文字框
ResultTextBox.Clear();
while (reader.Read())
{
// 逐行讀取查詢結果,並顯示在結果文字框中
for (int i = 0; i < reader.FieldCount; i++)
{
ResultTextBox.AppendText(reader[i].ToString() + "\t");
}
ResultTextBox.AppendText(Environment.NewLine);
}
reader.Close();
}
}
catch (Exception ex)
{
MessageBox.Show($"查詢執行失敗:{ex.Message}", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// 如果使用者按下Enter鍵,執行查詢
private void QueryTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ExecuteQueryButton_Click(sender, e);
}
}
}
}