c#操作sqlserver數(shù)據(jù)庫(kù)的簡(jiǎn)單示例
1.在用windows模式登陸sql server 數(shù)據(jù)庫(kù) 簡(jiǎn)歷一個(gè)student的數(shù)據(jù)庫(kù),然后新建查詢:
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ù)庫(kù)中的數(shù)據(jù)
相關(guān)文章
C#?Winform實(shí)現(xiàn)圓角無(wú)鋸齒按鈕
這篇文章主要介紹了C#?Winform實(shí)現(xiàn)圓角無(wú)鋸齒按鈕,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
.net使用Aspose.Words進(jìn)行Word替換操作的實(shí)現(xiàn)代碼
之前在工作中,需要實(shí)現(xiàn)Word打印功能,并且插入圖片。當(dāng)時(shí)采取的方式則是使用書簽進(jìn)行操作。首先在word內(nèi)插入書簽,完成后,存為模板。程序加載該模板,找到書簽,并在指定位置寫入文字即可2013-05-05
unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
這篇文章主要介紹了unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04

