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