C#連接到sql server2008數(shù)據(jù)庫(kù)的實(shí)例代碼
更新時(shí)間:2017年09月05日 14:12:47 作者:醉墨重生
這篇文章主要介紹了C#連接到sql server2008數(shù)據(jù)庫(kù)的實(shí)例代碼,需要的朋友可以參考下
廢話不多說(shuō)了,直接給大家貼代碼了,具體代碼如下所示:
namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = null;
SqlCommand comm = null;
SqlDataReader sdreader = null;
try
{
string ConStr = "server=192.168.1.110;uid=sa;pwd=woaifr0828;database=shudiansuo";
conn = new SqlConnection(ConStr);
conn.Open();
string sql = "select * from dbo.Member";
comm = new SqlCommand(sql);
comm.Connection = conn;
sdreader = comm.ExecuteReader();
sdreader.Read();
Console.WriteLine("數(shù)據(jù)庫(kù)連接成功!");
Console.WriteLine("student表的內(nèi)容");
while (sdreader.Read())
{
Console.WriteLine("sdreader[\"id\"]=" + sdreader["Id"]);
Console.WriteLine("sdreader[\"name\"]=" + sdreader["MemberName"]);
Console.WriteLine("sdreader[\"sex\"]=" + sdreader["PhoneNum"]);
}
sdreader.Close();
conn.Close();
}
catch (SqlException e)
{
Console.WriteLine("A SqlException was thrown");
Console.WriteLine("Number = " + e.Number);
Console.WriteLine("Message = " + e.Message);
Console.WriteLine("StackTrace:\n" + e.StackTrace);
}
Console.ReadKey();
}
}
}
總結(jié)
以上所述是小編給大家介紹的C#連接到sql server2008數(shù)據(jù)庫(kù)的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
您可能感興趣的文章:
- c#連接sqlserver數(shù)據(jù)庫(kù)、插入數(shù)據(jù)、從數(shù)據(jù)庫(kù)獲取時(shí)間示例
- C#實(shí)現(xiàn)連接SQL Server2012數(shù)據(jù)庫(kù)并執(zhí)行SQL語(yǔ)句的方法
- C#編程實(shí)現(xiàn)連接SQL SERVER數(shù)據(jù)庫(kù)實(shí)例詳解
- C#連接SQL Server的實(shí)現(xiàn)方法
- C#使用SqlConnection連接到SQL Server的代碼示例
- C#實(shí)現(xiàn)異步連接Sql Server數(shù)據(jù)庫(kù)的方法
- 關(guān)于C#連接SQL Server時(shí)提示用戶登錄失敗的解決方法
- C#連接SQL Server數(shù)據(jù)庫(kù)的實(shí)例講解
- 使用C#連接SQL?Server的詳細(xì)圖文教程
- C#連接SQL?Sever數(shù)據(jù)庫(kù)詳細(xì)圖文教程
相關(guān)文章
C#中Override關(guān)鍵字和New關(guān)鍵字的用法詳解
這篇文章主要介紹了C#中Override關(guān)鍵字和New關(guān)鍵字的用法,需要的朋友可以參考下2016-01-01
字符串和十六進(jìn)制之間的轉(zhuǎn)換方法實(shí)例
這篇文章介紹了字符串和十六進(jìn)制之間的轉(zhuǎn)換方法實(shí)例,有需要的朋友可以參考一下2013-11-11

