C#簡(jiǎn)單連接sql數(shù)據(jù)庫(kù)的方法
本文實(shí)例講述了C#簡(jiǎn)單連接sql數(shù)據(jù)庫(kù)的方法。分享給大家供大家參考,具體如下:
using System; using System.Collections.Generic; using System.Text; //數(shù)據(jù)庫(kù)操作對(duì)象庫(kù) 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("請(qǐng)輸入Data Source:"); string data_source = Console.ReadLine(); Console.WriteLine("請(qǐng)輸入Initial Catalog:"); string initial_catalog = Console.ReadLine(); Console.WriteLine("請(qǐng)輸入user id:"); string user_id = Console.ReadLine(); Console.WriteLine("請(qǐng)輸入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賬戶(hù)密碼 */ 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對(duì)象 conn = new SqlConnection(strConn); //打開(kāi)數(shù)據(jù)庫(kù)連接 conn.Open(); //創(chuàng)建Transac Sql命令對(duì)象 sqlCmd = conn.CreateCommand(); //創(chuàng)建建表語(yǔ)句 //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)容感興趣的讀者可查看本站專(zhuān)題:《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
欄 目:C#教程
本文標(biāo)題:C#簡(jiǎn)單連接sql數(shù)據(jù)庫(kù)的方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6447.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單的Login窗口實(shí)例
- 01-10Winform消除button按下出現(xiàn)的虛線簡(jiǎn)單實(shí)現(xiàn)方法
- 01-10winform簡(jiǎn)單緩存類(lèi)實(shí)例
- 01-10C#一個(gè)簡(jiǎn)單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10C#圓角窗體簡(jiǎn)單實(shí)現(xiàn)方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法
- 01-10C#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法
- 01-10基于C#實(shí)現(xiàn)簡(jiǎn)單離線注冊(cè)碼生成與驗(yàn)證


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文