C#簡單連接sql數(shù)據(jù)庫的方法
本文實(shí)例講述了C#簡單連接sql數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
using System; using System.Collections.Generic; using System.Text; //數(shù)據(jù)庫操作對象庫 using System.Data; using System.Data.SqlClient; using worddic; namespace testDB { class Program { static void Main(string[] args) { //char[] array = new char[2] ; string [] a = new string[2]; FileOp words = new FileOp(); words.openfile("worddic.txt"); words.getwords(ref a); /* Console.WriteLine("請輸入Data Source:"); string data_source = Console.ReadLine(); Console.WriteLine("請輸入Initial Catalog:"); string initial_catalog = Console.ReadLine(); Console.WriteLine("請輸入user id:"); string user_id = Console.ReadLine(); Console.WriteLine("請輸入pass word:"); string pword = Console.ReadLine(); //連接字符串 string strConn ="Data Source="+data_source+";Initial Catalog="+initial_catalog+";User ID="+user_id+";Password="+pword+"";//YourPwd替換為你設(shè)置的sa賬戶密碼 */ string strConn = "Data Source=HYPER-V-WIN2003\\SQLSRV2005;Initial Catalog=Mytest;User ID=sa;Password=sa"; SqlConnection conn = null; SqlCommand sqlCmd = null; try { //創(chuàng)建connection對象 conn = new SqlConnection(strConn); //打開數(shù)據(jù)庫連接 conn.Open(); //創(chuàng)建Transac Sql命令對象 sqlCmd = conn.CreateCommand(); //創(chuàng)建建表語句 //sqlCmd.CommandText = "create table wordlist(wrongwords varchar(30),rightwords varchar(30),sign char(20),)"; //sqlCmd.ExecuteScalar(); while (a[0] != null) { sqlCmd.CommandText ="insert into wordlist(wrongwords,rightwords,sign) values('" + a[0] + "','" + a[1] + "',1 )"; sqlCmd.ExecuteScalar(); a[0] = null; a[1] = null; words.getwords(ref a); } words.fileclose(); Console.WriteLine(); //打印所有記錄 } catch (SqlException e) { Console.WriteLine(e.Message); } finally { conn.Close(); } Console.WriteLine("程序結(jié)束,按任意鍵退出"); Console.ReadKey(); } } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)簡單的Login窗口實(shí)例
- 01-10Winform消除button按下出現(xiàn)的虛線簡單實(shí)現(xiàn)方法
- 01-10winform簡單緩存類實(shí)例
- 01-10C#一個(gè)簡單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10C#圓角窗體簡單實(shí)現(xiàn)方法
- 01-10C#使用Mutex簡單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10C#實(shí)現(xiàn)簡單合并word文檔的方法
- 01-10C#簡單實(shí)現(xiàn)子窗體向父窗體傳值的方法
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫中的方法
- 01-10基于C#實(shí)現(xiàn)簡單離線注冊碼生成與驗(yàn)證


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文