c#操作sqlserver數(shù)據(jù)庫的簡單示例
1.在用windows模式登陸sql server 數(shù)據(jù)庫 簡歷一個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中新建一個項目,輸入一下代碼:
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("鏈接錯誤:" + 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.運行結(jié)果將會顯示數(shù)據(jù)庫中的數(shù)據(jù)
相關(guān)文章
.net使用Aspose.Words進行Word替換操作的實現(xiàn)代碼
之前在工作中,需要實現(xiàn)Word打印功能,并且插入圖片。當(dāng)時采取的方式則是使用書簽進行操作。首先在word內(nèi)插入書簽,完成后,存為模板。程序加載該模板,找到書簽,并在指定位置寫入文字即可2013-05-05
unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周
這篇文章主要介紹了unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04

