您好,登錄后才能下訂單哦!
本篇內容主要講解“C#數據庫查詢和操作方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C#數據庫查詢和操作方法”吧!
一:C#數據庫查詢之數據庫連接代碼:
SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open();
二:數據庫的添加記錄代碼:
inti=0; strings1="",s2=""; i=Convert.ToInt16(textBox1.Text); s1=textBox2.Text; s2=textBox3.Text; SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("數據庫連接成功","好"); try { SqlCommandsqlcom=newSqlCommand("insertintoinfo(id,name,sex)values("+i+",'"+s1+"','"+s2+"')",objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("添加成功!","啊"); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } MessageBox.Show("添加成功!","啊"); }
三:數據庫的修改代碼:
inti=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("數據庫連接成功","好"); try { SqlCommandsqlcom=newSqlCommand("updateinfosetname='"+s1+"',sex='"+s2+"'"+"whereid="+i,objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("修改成功!","啊"); objSqlConnection.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); }
四:數據庫的刪除代碼:
inti=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt16(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("數據庫連接成功","好"); try { SqlCommandsqlcom=newSqlCommand("deletefrominfowhereid="+i,objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("刪除成功!","啊"); objSqlConnection.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); }
五:數據庫的查詢代碼:
1.類開始:
DataTabledt1=newDataTable(); SqlDataAdapterda1=newSqlDataAdapter();
2.按鈕代碼:
inti=0,n=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("數據庫連接成功","好"); stringquery="SELECT*frominfowhereid="+i; DataSetobjDataSet=newDataSet(); SqlDataAdapterobj=newSqlDataAdapter(); obj.SelectCommand=newSqlCommand(query,objSqlConnection); obj.Fill(objDataSet,"info"); SqlCommandobjSqlCommand=newSqlCommand(query,objSqlConnection); SqlDataReaderobjSqlReader=objSqlCommand.ExecuteReader(); while(objSqlReader.Read()) { n+=1; MessageBox.Show("編號:"+objSqlReader.Getvalue(0)+"姓名:"+objSqlReader.Getvalue(1)+"性別"+objSqlReader.Getvalue(2)); } if(n==0) MessageBox.Show("數據庫中沒有這樣的記錄!");
六:C#數據庫查詢代碼:
inti=0; //intn=0; strings1="",s2=""; stringsql; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) { i=0; } else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("數據庫連接成功","好"); stringquery="SELECT*frominfowhereid="+i; if(i==0) sql="select*frominfo"; else sql="select*frominfowhereid="+i; da1=newSqlDataAdapter(sql,objSqlConnection); dt1.Clear(); da1.Fill(dt1); dataGridView1.DataSource=dt1;
C#數據庫查詢之數據庫的封裝類代碼:
classDBClass { publicvoiddbclass(stringsql) { try { SqlConnectionsqlcon=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); sqlcon.Open(); SqlTransactionobjt=sqlcon.BeginTransaction();//事物開始 SqlCommandsqlcom=newSqlCommand(sql,sqlcon); sqlcom.Transaction=objt;//將Command對象設置為事物處理的對象 sqlcom.ExecuteNonQuery(); objt.Commit();//提交事物 sqlcon.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } } } --db2數據庫連接代碼: stringstrcon="Provider=IBMDADB2;DataSource=hfzd;UserId=db2admin;Password=db2admin"; //stringsql="select*fromADMINISTRATOR.HFZD"; stringsql="deletefromADMINISTRATOR.HFZDwhereID=1"; OleDbConnectionolecon=newOleDbConnection(strcon); olecon.Open(); MessageBox.Show("數據庫已連接上"); dt.Clear(); da=newOleDbDataAdapter(sql,olecon); da.Fill(dt); dataGridView1.DataSource=dt; olecon.Close();
到此,相信大家對“C#數據庫查詢和操作方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。