今天資料庫實驗是“掌握資料庫應用開發的一般方法”,開發環境是vs(我這里用的是19版)
一、準備
- 操作環境:win7以上
- DBMS:MySQL 5.5
- 開發環境:vs19(12及以上都可以)
二、建立工程專案
新建工程后默認模板
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MySql.Data.MySqlClient;
namespace LiwkerApp
{
class Program
{
static void Main(string[] args)
{
}
}
}
后面就不多說了,一定要先匯入程式包,再安裝

三、實驗
1.隨機查詢一個學生
static void Main(string[] args)
{
//定義好功能函式后,在main呼叫
//show();
//delOne();
//Find();
//showAll();
//addOne1();
}
代碼
//顯示學生的函式
static void show()
{
// 定義一個連接到資料庫的字串
// 這里連接的是本地服務器(localhost),登錄用戶名是root,有密碼就加
string conStr = "server=localhost;user=root;database=Liwker;password=1110";
// 定義一個資料庫連接物件con,可以理解它是連接程式連接資料庫的通道
// 這里表明用上面的連接串來建立與目標資料庫的連接
MySqlConnection con = new MySqlConnection(conStr);
// 連接到資料庫,即通常所說的打開資料庫
// 注意如果運行時在這里報錯,要么是服務器沒啟動
// 要么就是前面的 conStr中的連接資訊寫錯了!
con.Open();
// 定義一個S0L命令物件,用于存盤給服務器發送的SQL命令及引數等資訊
MySqlCommand cmd = new MySqlCommand();
// 該SQL命令物件通過con連接到我們的資料庫
cmd.Connection = con;
// 該SQL命令物件將向服務器發送命令字串,當然可以使用存盤程序等其它型別
cmd.CommandType = System.Data.CommandType.Text;
// 編制將要執行的SQL命令,這里命令的功能是隨機取回一名學生的姓名和性別
cmd.CommandText = "select * from student order by rand() limit 1;";
// 定義一個基于cmd的資料配接器,它專用于接收cmd命令執行后的結果
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
// 定義一個資料集,相當于表的集合, 用于接收SQL命令物件執行后的結果
DataSet ds = new DataSet();
// 用配接器執行命令來填充ds資料集,即將取回的資料裝入ds
adapter.Fill(ds); // 這個方法是有回傳值的,回傳裝入的行數
// 前面的SQL命令成功取回 學生的資訊后會放入ds的tables[0]表的rows[0]行
// 沒有的話,則tables[0]表的rows的行數為0
if (ds.Tables[0].Rows.Count > 0)
{
System.Console.WriteLine("學號: {0}", ds.Tables[0].Rows[0]["學號"]);
// 顯示回傳的姓名
System.Console.WriteLine("姓名: {0}", ds.Tables[0].Rows[0]["姓名"]);
// 顯示回傳的性別
System.Console.WriteLine("性別: {0}", ds.Tables[0].Rows[0]["性別"]);
System.Console.WriteLine("年齡: {0}", ds.Tables[0].Rows[0]["年齡"]);
System.Console.WriteLine("系別: {0}", ds.Tables[0].Rows[0]["系別"]);
}
else
{
System.Console.WriteLine("目前還沒有學生!");
}
// 命令用完了,記得關閉連接,以便服務器更好地為其它連接服務
con.Close();
}
結果

2.洗掉學生
代碼
// 通過學號洗掉一個學生
static void delOne()
{
string who; // 存盤查找的學號
Console.Write("洗掉者學號:"); //輸出提示資訊
who = Console.ReadLine(); //接受鍵盤輸入
// 任何資料庫操作,其連接資料庫以及SQL命令的準備都是一樣的,所以可以直接用下面的
string conStr = "server=localhost;user=root;database=Liwker;password=1110";
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
// 編輯發出給服務器的洗掉學生的SQL命令,其中@xh是一個引數
cmd.CommandText = "delete from student where 學號=@xh";
//提供命令所需要的引數,這里提供剛才輸入的學號
cmd.Parameters.AddWithValue("@xh", who);
//非查詢類的SQL命令的執行用以下方法,回傳1 代表成功,0則失敗
int r = cmd.ExecuteNonQuery();
if(r == 1)
{
System.Console.WriteLine("洗掉成功!");
}
else
{
System.Console.WriteLine("洗掉失敗,也可能是根本沒有此人!");
}
// 命令用完了,記得關閉連接,以便服務器更好地為其它連接服務
con.Close();
}
//通過學號查詢一個學生的資訊
static void Find()
{
string who;
Console.Write("請輸入學號查詢:");
who = Console.ReadLine();
string conStr = "server=localhost;user=root;database=Liwker;password=1110"; // 連接資料庫的字串
MySqlConnection con = new MySqlConnection(conStr); // 定義一個資料庫新鏈接
con.Open(); // 打開資料庫
MySqlCommand cmd = new MySqlCommand(); // 定義一個新SQL命令
cmd.Connection = con; // SQL命令鏈接到資料庫
cmd.CommandType = System.Data.CommandType.Text; // 發送命令字串
cmd.CommandText = "select * from student where 學號=@xh;"; // 寫入SQL命令字串
cmd.Parameters.AddWithValue("@xh", who); // 替換引數
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); // 定義一個接受cmd物件執行結果的資料配接器
DataSet ds = new DataSet(); // 定義一個接受SQL物件執行結果的資料集
adapter.Fill(ds); // 將配接器里的資料裝入ds
if (ds.Tables[0].Rows.Count > 0)
{
System.Console.WriteLine("學號: {0}", ds.Tables[0].Rows[0]["學號"]);
System.Console.WriteLine("姓名: {0}", ds.Tables[0].Rows[0]["姓名"]);
System.Console.WriteLine("性別: {0}", ds.Tables[0].Rows[0]["性別"]);
System.Console.WriteLine("年齡: {0}", ds.Tables[0].Rows[0]["年齡"]);
System.Console.WriteLine("系別: {0}", ds.Tables[0].Rows[0]["系別"]);
}
else
{
System.Console.WriteLine("查無此人!");
}
con.Close();
}
結果

3.查詢所有學生資訊
代碼
// 查詢所有學生的資訊
static void showAll()
{
string conStr = "server=localhost;user=root;database=Liwker;password=1110";
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "select * from student;";
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
// 利用回圈來遍歷所有的行(學生)
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
System.Console.WriteLine("學號: {0}", ds.Tables[0].Rows[i]["學號"]);
System.Console.WriteLine("姓名: {0}", ds.Tables[0].Rows[i]["姓名"]);
System.Console.WriteLine("性別: {0}", ds.Tables[0].Rows[i]["性別"]);
System.Console.WriteLine("年齡: {0}", ds.Tables[0].Rows[i]["年齡"]);
System.Console.WriteLine("系別: {0}", ds.Tables[0].Rows[i]["系別"]);
System.Console.WriteLine("\n");
}
con.Close();
}
結果

4.查詢單個學生
代碼
//通過學號查詢一個學生的資訊
static void Find()
{
string who;
Console.Write("請輸入學號查詢:");
who = Console.ReadLine();
string conStr = "server=localhost;user=root;database=Liwker;password=1110"; // 連接資料庫的字串
MySqlConnection con = new MySqlConnection(conStr); // 定義一個資料庫新鏈接
con.Open(); // 打開資料庫
MySqlCommand cmd = new MySqlCommand(); // 定義一個新SQL命令
cmd.Connection = con; // SQL命令鏈接到資料庫
cmd.CommandType = System.Data.CommandType.Text; // 發送命令字串
cmd.CommandText = "select * from student where 學號=@xh;"; // 寫入SQL命令字串
cmd.Parameters.AddWithValue("@xh", who); // 替換引數
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); // 定義一個接受cmd物件執行結果的資料配接器
DataSet ds = new DataSet(); // 定義一個接受SQL物件執行結果的資料集
adapter.Fill(ds); // 將配接器里的資料裝入ds
if (ds.Tables[0].Rows.Count > 0)
{
System.Console.WriteLine("學號: {0}", ds.Tables[0].Rows[0]["學號"]);
System.Console.WriteLine("姓名: {0}", ds.Tables[0].Rows[0]["姓名"]);
System.Console.WriteLine("性別: {0}", ds.Tables[0].Rows[0]["性別"]);
System.Console.WriteLine("年齡: {0}", ds.Tables[0].Rows[0]["年齡"]);
System.Console.WriteLine("系別: {0}", ds.Tables[0].Rows[0]["系別"]);
}
else
{
System.Console.WriteLine("查無此人!");
}
con.Close();
}
結果

5.新增一個學生
方法一
// 新增學生(方法一)
// 有點缺陷,因為學號屬性是固定的5個字符,這里就用了 if 來判斷比較(所以查出來的有空格)
// 例如:輸入“S02”是找不到的,必須輸入“S02 ”
static void addOne()
{
string xh, name, sex, age, xb, sg; // 宣告接收學生資訊的不同字串
Console.Write("新增者\n學號:");
xh = Console.ReadLine(); // 先輸入學號,以便判斷是否有重復
// 連接資料庫
string conStr = "server=localhost;user=root;database=Liwker;password=1110";
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
// 先獲取已有的學生的學號
cmd.CommandText = "select 學號 from student";
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
// 學號重復判斷
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (xh == (string)ds.Tables[0].Rows[i]["學號"]) //加string強制轉換
{
Console.Write("此學號已存在,請重新輸入:\n");
xh = Console.ReadLine();
i = -1;
}
}
Console.Write("姓名:");
name = Console.ReadLine();
Console.Write("性別:");
sex = Console.ReadLine();
Console.Write("年齡:");
age = Console.ReadLine();
Console.Write("系別:");
xb = Console.ReadLine();
Console.Write("身高:");
sg = Console.ReadLine();
// 定義SQL插入命令
cmd.CommandText = "insert into student values(@xh,@name,@sex,@age,@xb,@sg)";
cmd.Parameters.AddWithValue("@xh", xh);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@sex", sex);
cmd.Parameters.AddWithValue("@age", age);
cmd.Parameters.AddWithValue("@xb", xb);
cmd.Parameters.AddWithValue("@sg", sg);
int r = cmd.ExecuteNonQuery(); // 非查詢類SQL命令的回傳(0 或 1)
if (r == 1)
{
System.Console.WriteLine("新增成功!");
}
else
{
System.Console.WriteLine("新增失敗!");
}
con.Close();
}
方法二
// 新增學生(方法二)
// 此方法就不存在空格問題
// 每輸入學號就會運行SQL查看有沒有重復的
static void addOne1()
{
string xh, name, sex, age, xb, sg;
int f = 0; // 定義一個標志,以便后面判斷重復
Console.Write("新增者\n學號:");
xh = Console.ReadLine();
// 連接資料庫
string conStr = "server=localhost;user=root;database=Liwker;password=1110";
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
// 學號重復判斷
do
{
// 先來判斷是否有重復
if (f != 0)
{
Console.Write("此學號已存在,請重新輸入:\n");
xh = Console.ReadLine();
}
// 定義一個新的SQL命令
MySqlCommand cmd1 = new MySqlCommand();
cmd1.Connection = con;
cmd1.CommandType = System.Data.CommandType.Text;
// 定義查詢這個學號是否重復的SQL命令
cmd1.CommandText = "select * from student where 學號=@xh";
cmd1.Parameters.AddWithValue("@xh", xh);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd1);
DataSet ds = new DataSet();
f = adapter.Fill(ds); // f接受行數,0為沒有,n為有n行資料
} while (f > 0); // 一直回圈到沒有重復為止
Console.Write("姓名:");
name = Console.ReadLine();
Console.Write("性別:");
sex = Console.ReadLine();
Console.Write("年齡:");
age = Console.ReadLine();
Console.Write("系別:");
xb = Console.ReadLine();
Console.Write("身高:");
sg = Console.ReadLine();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
// 定義SQL插入命令
cmd.CommandText = "insert into student values(@xh,@name,@sex,@age,@xb,@sg)";
cmd.Parameters.AddWithValue("@xh", xh);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@sex", sex);
cmd.Parameters.AddWithValue("@age", age);
cmd.Parameters.AddWithValue("@xb", xb);
cmd.Parameters.AddWithValue("@sg", sg);
int r = cmd.ExecuteNonQuery(); // 非查詢類SQL命令的回傳(0 或 1)
if (r == 1)
{
System.Console.WriteLine("\n新增成功!");
}
else
{
System.Console.WriteLine("\n新增失敗!");
}
con.Close();
}
結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/231252.html
標籤:.NET技术
上一篇:C#(六)基礎篇—陣列
