c#操作sqlserver數(shù)據(jù)庫的簡(jiǎn)單示例
1.在用windows模式登陸sql server 數(shù)據(jù)庫 簡(jiǎn)歷一個(gè)student的數(shù)據(jù)庫,然后新建查詢:
create table student
(
id int auto_increment primary key,
name char(10) not null,
sex char(10) not null,
age char(10) not null,
)
2.在vs中新建一個(gè)項(xiàng)目,輸入一下代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connSting;
connSting = "server=localhost;database=student;Integrated Security=True ";
SqlConnection sConn = new SqlConnection(connSting);
try
{
sConn.Open();
}
catch (Exception ex)
{
Console.WriteLine("鏈接錯(cuò)誤:" + ex.Message);
}
string sqlMessage=null;
sqlMessage = "select * from student";
SqlCommand sCmd = new SqlCommand(sqlMessage, sConn);
SqlDataReader sdr = null;
try
{
sdr = sCmd.ExecuteReader();
Console.WriteLine(" 姓名 性別 年齡 ");
while (sdr.Read())
{
Console.WriteLine(sdr["name"] +""+ sdr["sex"]+"" + sdr["age"]);
}
sConn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
3.運(yùn)行結(jié)果將會(huì)顯示數(shù)據(jù)庫中的數(shù)據(jù)
相關(guān)文章
深入解析C#設(shè)計(jì)模式編程中對(duì)建造者模式的運(yùn)用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中對(duì)建造者模式的運(yùn)用,文中還介紹了在.NET框架下建造者模式編寫思路的實(shí)現(xiàn),需要的朋友可以參考下2016-02-02Unity游戲開發(fā)中的設(shè)計(jì)模式之策略模式
策略模式是Unity游戲開發(fā)中常用的設(shè)計(jì)模式之一,用于封裝一系列算法或行為,并使這些算法或行為可以相互替換。通過策略模式,可以在運(yùn)行時(shí)動(dòng)態(tài)地選擇算法或行為,實(shí)現(xiàn)游戲中的多樣性和可擴(kuò)展性。常見的應(yīng)用包括AI行為、武器攻擊、移動(dòng)方式等2023-05-05C# Winform實(shí)現(xiàn)石頭剪刀布游戲
這篇文章主要為大家詳細(xì)介紹了Winform實(shí)現(xiàn)石頭剪刀布游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01C#枚舉類型與結(jié)構(gòu)類型實(shí)例解析
這篇文章主要介紹了C#枚舉類型與結(jié)構(gòu)類型實(shí)例,需要的朋友可以參考下2014-07-07