如何在VisualStudio中連接SQLServer數(shù)據(jù)庫-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)如何在Visual Studio中連接SQLServer數(shù)據(jù)庫,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

成都創(chuàng)新互聯(lián)專注于邢臺縣企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),購物商城網(wǎng)站建設(shè)。邢臺縣網(wǎng)站建設(shè)公司,為邢臺縣等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

一、Sql Server 在Visual Studio的連接有兩種方法:
(1)本地計(jì)算機(jī)連接;


string s = "Data Source=計(jì)算機(jī)名稱;initial Catalog=數(shù)據(jù)庫名稱;integrated Security=True";


(2)windows身份驗(yàn)證方式連接;


string cc="Data Source = 計(jì)算機(jī)名稱; Initial Catalog = 數(shù)據(jù)庫名稱; User ID = sa; Password = 你的密碼";


二、在Visual Studio中使用:
例1:查詢數(shù)據(jù)庫中的數(shù)據(jù)并且顯示出來


string s = "Data Source=計(jì)算機(jī)名稱;Initial Catalog=數(shù)據(jù)庫名稱;Integrated Security=True";  //此處使用本地計(jì)算機(jī)連接方式  
SqlConnection conn = new SqlConnection(s);   //創(chuàng)建連接  
conn.Open();    //打開連接  
SqlCommand cmd = conn.CreateCommand();  
cmd.CommandText = "select * from T_User";   //使用命令  
SqlDataAdapter adapter=new SqlDataAdapter(cmd);  
DataTable dt=new DataTable();  
adapter.Fill(dt);  
conn.Dispose();  //釋放所以資源  
cmd.Dispose();  
conn.Close();  //關(guān)閉連接  
string realname="";  
string username="";  
string mobile="";  
string address="";  
for (int i=0;i<dt.Rows.Count;i++)  
{  
    realname=dt.Rows[i][3].ToString();  
    username=dt.Rows[i][1].ToString();  
    mobile=dt.Rows[i][4].ToString();  
    address=dt.Rows[i][5].ToString();  
    Console.WriteLine("姓名為{0},用戶名為{1},手機(jī)為{2},地址為{3}", realname, username, mobile, address);  
}  
Console.ReadKey();

例2:刪除表中數(shù)據(jù)


string cc="Data Source = 計(jì)算機(jī)名稱; Initial Catalog = 數(shù)據(jù)庫名稱; User ID = sa; Password = 你的密碼";   //使用windows身份驗(yàn)證  
SqlConnection conn = new SqlConnection(s);  
conn.Open();  
SqlCommand cmd = conn.CreateCommand();  
cmd.CommandText = "delete from T_User where Id=5";  
cmd.ExecuteNonQuery();  
cmd.Dispose();  
conn.Close();  
Console.WriteLine("刪除成功");  
Console.ReadKey();


例3:修改表中數(shù)據(jù)


string s = "Data Source=計(jì)算機(jī)名稱;initial Catalog=數(shù)據(jù)庫名稱;integrated Security=True";  
SqlConnection conn = new SqlConnection(s);  
conn.Open();  
SqlCommand cmd = conn.CreateCommand();  
cmd.CommandText = "update T_User set Card=@card where ID=3";  
cmd.Parameters.AddWithValue("@card", "13000000000000");  
cmd.ExecuteNonQuery();  
cmd.Dispose();  
conn.Close();  
conn.Dispose();  
Console.WriteLine("修改成功!");  
Console.ReadKey();


例4:向表中插入數(shù)據(jù)


string s = "data source=計(jì)算機(jī)名稱;initial catalog=數(shù)據(jù)庫名稱;integrated security=true";  
SqlConnection conn = new SqlConnection(s);  
conn.Open();  
SqlCommand cmd = conn.CreateCommand();  
cmd.CommandText = "insert into T_User(UserName,Password,RealName,Mobile,Address) values(@username,@password,@realname,@mobile,@address)";  
cmd.Parameters.AddWithValue("@username", "xingxing");  
cmd.Parameters.AddWithValue("@password", "77777");  
cmd.Parameters.AddWithValue("@realname", "星星");  
cmd.Parameters.AddWithValue("@mobile", 1300000000);  
cmd.Parameters.AddWithValue("@address", "河北省北京市");  
cmd.ExecuteNonQuery();  
cmd.Dispose();  
conn.Close();  
conn.Dispose();  
Console.WriteLine("成功插入一行");  
Console.ReadKey();

看完上述內(nèi)容,你們對如何在Visual Studio中連接SQLServer數(shù)據(jù)庫有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享文章:如何在VisualStudio中連接SQLServer數(shù)據(jù)庫-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article28/dhiejp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站網(wǎng)站導(dǎo)航、網(wǎng)站排名App開發(fā)、商城網(wǎng)站、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名