工控最強(qiáng)王者
世上無(wú)難事,只怕有心人
級(jí)別: 略有小成
![]() |
折騰了好久,蒙圈了很久,終于調(diào)用數(shù)據(jù)庫(kù)成功,小白一個(gè),把學(xué)習(xí)經(jīng)驗(yàn)分享一下,,希望高手指點(diǎn)。。。 首先你要有C#基礎(chǔ)吧,http://www.runoob.com/csharp/csharp-operators.html;其次你要知道數(shù)據(jù)庫(kù)是干嘛用的,可以百度搜; 安裝VS2017;這個(gè)是C#開(kāi)發(fā)環(huán)境,也可以弄數(shù)據(jù)庫(kù); ![]() 然后就新建一個(gè)C#桌面應(yīng)用,畫一個(gè)按鈕 ![]() 數(shù)據(jù)庫(kù)怎么弄呢,https://jingyan.baidu.com/album/9f63fb91893ac3c8410f0e58.html?picindex=1; 窗體應(yīng)用怎么連接數(shù)據(jù)庫(kù)呢 https://www.cnblogs.com/makqiq/p/5882351.html 下圖是我設(shè)置的表,以及窗體查詢數(shù)據(jù)庫(kù)里的數(shù)據(jù) ![]() 點(diǎn)擊運(yùn)行 ![]() ![]() ![]() 下面附上程序 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 WindowsFormsApp4 { public partial class Form1 : Form //窗體1 { private string connectString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\123\Documents\mydata.mdf;Integrated Security=True;Connect Timeout=30";//這個(gè)是連接數(shù)據(jù)庫(kù)的字符串,右擊你建立的數(shù)據(jù)庫(kù),屬性,連接字符串復(fù)制過(guò)來(lái),記得加上@哦 public Form1() { InitializeComponent();//初始化 } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) //按鈕點(diǎn)擊事件 { SqlConnection sqlCnt = new SqlConnection(connectString);//實(shí)例化sqlConnection sqlCnt.Open(); //打開(kāi)數(shù)據(jù)庫(kù) MessageBox.Show("數(shù)據(jù)庫(kù)已打開(kāi)");//打印數(shù)據(jù)庫(kù) SqlCommand command = sqlCnt .CreateCommand();//實(shí)例化SqlCommand command.CommandType = CommandType.Text; //這個(gè)是執(zhí)行SQL語(yǔ)句 command.CommandText = "SELECT*FROM dbo.[Table]"; //查詢你建立的表格 SqlDataReader reader = command.ExecuteReader(); //執(zhí)行SQL,返回一個(gè)“流” while (reader.Read()) { MessageBox.Show(Convert.ToString ( reader["id"])+ Convert.ToString(reader["姓名"]) + Convert.ToString(reader["年齡"])); // 打印出每個(gè)用戶的信息 } sqlCnt.Close();//關(guān)閉數(shù)據(jù)庫(kù) } } } [ 此帖被工控最強(qiáng)王者在2019-01-22 16:18重新編輯 ] |
---|---|
本帖最近評(píng)分記錄: |
walxyfsk
級(jí)別: 探索解密
![]() |
這個(gè)不錯(cuò),我也正想學(xué)習(xí)這個(gè) |
---|---|
|
你好啊朋友
級(jí)別: 略有小成
![]() |
雖說(shuō)現(xiàn)在用不到,但以后指不定就想學(xué)了。經(jīng)驗(yàn)互享,共同進(jìn)步,老鐵,狠贊,給你雙擊666! |
|
---|---|---|
|
zw2940707
級(jí)別: 探索解密
![]() |
太一、定義連接字符串,用來(lái)鏈接SQL Server string str_con = "server=.(服務(wù)器名稱一般為 . );database=WordBook(數(shù)據(jù)庫(kù)名稱);uid=sa(服務(wù)器登錄名);pwd=123(服務(wù)器密碼)"; 二、有了鏈接字符串之后,開(kāi)始數(shù)據(jù)庫(kù)操作 1、數(shù)據(jù)庫(kù)查詢 定義了一個(gè)查詢方法,用來(lái)調(diào)用: public DataSet queryDatabase(string sql) //sql是查詢語(yǔ)句 { //儲(chǔ)存數(shù)據(jù)的工具初始化 DataSet ds = new DataSet(); //相當(dāng)于鏈接數(shù)據(jù)庫(kù)的一個(gè)工具類(連接字符串) using (SqlConnection con = new SqlConnection(str_con)) { con.Open(); //打開(kāi) //用SqlConnection工具鏈接數(shù)據(jù)庫(kù),在通過(guò)sql查詢語(yǔ)句查詢結(jié)果現(xiàn)存入sql適配器 SqlDataAdapter sda = new SqlDataAdapter(sql,con); //(查詢語(yǔ)句和連接工具) sda.Fill(ds); //將適配器數(shù)據(jù)存入DataSet工具中 con.Close(); //用完關(guān)閉SqlConnection工具 return ds; } } 在需要查詢數(shù)據(jù)庫(kù)的地方調(diào)用此方法: private void query() { //查詢WordBook表中,book_key字段數(shù)值為7的那一行數(shù)據(jù) //string sql = "select * from Word_Book where book_key='7'"; string sql = "select * from Word_Book "; //查詢?nèi)?br /> DataSet ds = help.queryDatabase(sql); //查詢到數(shù)據(jù) DataTable dt = ds.Tables[0]; //把查到的數(shù)據(jù)存入數(shù)據(jù)表中 sqlDataResult.DataSource = dt; //把數(shù)據(jù)賦值給gridView展示(全表) // string str=dt.Rows[0][1].ToString();//查找表中某一個(gè)內(nèi)容 // MessageBox.Show(str); } 2、數(shù)據(jù)庫(kù)添加、刪除、修改 C#中數(shù)據(jù)庫(kù)的添加、刪除、修改用的是同斷代碼,所以定義了一個(gè)方法,用來(lái)調(diào)用: public int changeSqlData(String sql) { using(SqlConnection con=new SqlConnection(str_con)) { con.Open(); //操作數(shù)據(jù)庫(kù)的工具SqlCommand SqlCommand cmd = new SqlCommand(sql, con);//(操作語(yǔ)句和鏈接工具) int i=cmd.ExecuteNonQuery();//執(zhí)行操作返回影響行數(shù)() con.Close(); return i; } } 在需要操作數(shù)據(jù)庫(kù)的地方調(diào)用此方法: ①數(shù)據(jù)庫(kù)添加: private void btn_add_Click(object sender, EventArgs e) { //sql添加數(shù)據(jù) insert into 表名(字段,字段...) values(‘內(nèi)容’,‘內(nèi)容’...) string sql = "insert into Word_Book(book_word_CN,book_word_JP,book_word_Roma,book_nominal," + "book_gloze) values('" + book_word_CN.Text.Trim()+"','"+ book_word_JP .Text.Trim() + "','" + book_word_Roma .Text.Trim() + "','"+ book_nominal.Text.Trim() + "','" + book_gloze.Text.Trim() + "')"; int i=help.changeSqlData(sql); if (i == 0) MessageBox.Show("添加失敗", "提示:"); else MessageBox.Show("添加成功", "提示:"); } ②數(shù)據(jù)庫(kù)刪除: private void btn_delete_Click(object sender, EventArgs e) { //根據(jù)同個(gè)字段中不同內(nèi)容刪除多行 //delete from Word_Book where book_key in (1,2,3) //sql刪除數(shù)據(jù)delete 表名 where 字段='內(nèi)容'單個(gè)條件用or鏈接,多個(gè)條件用and鏈接 string sql = "delete from Word_Book where book_key='"+book_key.Text.Trim()+"'"; int i=help.changeSqlData(sql); if (i == 0) MessageBox.Show("刪除失敗", "提示:"); else MessageBox.Show("刪除成功", "提示:"); } ②數(shù)據(jù)庫(kù)更新: private void btn_update_Click(object sender, EventArgs e) { //根據(jù)條件修改多個(gè)字段內(nèi)容 //update 表名 set 字段='內(nèi)容', 字段='內(nèi)容' where 條件字段='內(nèi)容' string sql = "update Word_Book set book_word_CN='"+book_word_CN.Text.Trim()+ "', book_word_JP='"+book_word_JP.Text.Trim()+"'where book_key='" + book_key.Text.Trim()+"'"; int i = help.changeSqlData(sql); if (i == 0) MessageBox.Show("修改失敗", "提示:"); else MessageBox.Show("修改成功", "提示:"); } 樓主留言:int i =help. |
---|---|
本帖最近評(píng)分記錄: |
crgtom
人生三寶:家庭,事業(yè),健康。
級(jí)別: 網(wǎng)絡(luò)英雄
![]() ![]() |
![]() |
|
---|---|---|
|