---
# System prepended metadata

title: '主題:WinForm(C#)'

---

# 主題:WinForm(C#)
### 專案一.成績管理資訊系統
### 開發人員:國立台中科技大學 資訊工程科 五專部 學生
### 開發期間:2023/3/1~2023/6/20
#### 成員:資工四乙 陳德恩、資工三乙 劉依華、資工三乙 張家瑋
#### 資料庫連線辦法
```csharp=
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Management_Information_System
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //註冊區塊
        public void button1_Click(object sender, EventArgs e)
        {
            String st1=textBox1.Text; 
            String st2=textBox2.Text;
            SqlConnection connection = new SqlConnection(@"Data Source = localhost; Initial Catalog = user_information; Integrated Security=True; Connect Timeout=30; Encrypt=False;");//連線資料庫
            connection.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO user_data(account,password)VALUES ('"+st1+"','"+st2+"')", connection);//重要1
            SqlCommand run = new SqlCommand("SELECT * FROM user_data WHERE (account='" + st1 + "')AND (password ='" + st2 + "')", connection);//重要2
            SqlDataReader adapter = run.ExecuteReader();  
            if(adapter.HasRows==false)
            {
                MessageBox.Show("註冊完成");
                adapter.Close();
                cmd.ExecuteNonQuery();
            }
            else
            {
                adapter.Close();
                MessageBox.Show("註冊完成");
            }
            connection.Close();
        }
        //登入區塊
        public void button2_Click(object sender, EventArgs e)
        {
            String st1 = textBox1.Text;
            String st2 = textBox2.Text;
            SqlConnection connection = new SqlConnection("Data Source = localhost; Initial Catalog = user_information; Integrated Security=True; Connect Timeout=30; Encrypt=False;");//連線資料庫
            connection.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM user_data WHERE (account='"+st1+"')AND (password ='"+st2+"')", connection);//重要
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows==false)
            {
                MessageBox.Show("帳號或密碼錯誤");//注意資安問題
            }
            else
            {
                MessageBox.Show("登入成功");
                Form2 main=new Form2(); 
                this.Visible = false;
                main.Visible = true;
            }
            connection.Close();
        }
    }
}

```
