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賬戶密碼
*/
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)容感興趣的讀者可查看本站專題:《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
c#winform窗口頁(yè)面一打開(kāi)就加載的實(shí)現(xiàn)方式
這篇文章主要介紹了c#winform窗口頁(yè)面一打開(kāi)就加載的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
100行C#代碼實(shí)現(xiàn)經(jīng)典掃雷游戲
這篇文章主要為大家詳細(xì)介紹了如何用100行C#代碼實(shí)現(xiàn)經(jīng)典的掃雷游戲,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02
舉例說(shuō)明Java多線程編程中讀寫(xiě)鎖的使用
這篇文章主要介紹了舉例說(shuō)明Java多線程編程中讀寫(xiě)鎖的使用,文中的例子很好地說(shuō)明了Java的自帶讀寫(xiě)鎖ReentrantReadWriteLock的使用,需要的朋友可以參考下2016-02-02
C#實(shí)現(xiàn)計(jì)算器功能(winform版)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)winform版的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C#TreeView 無(wú)限級(jí)別分類實(shí)現(xiàn)方法
2013-04-04
詳解C# parallel中并行計(jì)算的四種寫(xiě)法總結(jié)
在C#中,parallel關(guān)鍵字可以用于并行計(jì)算。本文為大家總結(jié)了四種C# parallel并行計(jì)算的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11

