SQLServer 在Visual Studio的2種連接方法
更新時(shí)間:2013年09月06日 14:51:26 作者:
這篇文章介紹了SQLServer 在Visual Studio的2種連接方法,有需要的朋友可以參考一下
一、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();
(1)本地計(jì)算機(jī)連接;
復(fù)制代碼 代碼如下:
string s = "Data Source=計(jì)算機(jī)名稱;initial Catalog=數(shù)據(jù)庫名稱;integrated Security=True";
(2)windows身份驗(yàn)證方式連接;
復(fù)制代碼 代碼如下:
string cc="Data Source = 計(jì)算機(jī)名稱; Initial Catalog = 數(shù)據(jù)庫名稱; User ID = sa; Password = 你的密碼";
二、在Visual Studio中使用:
例1:查詢數(shù)據(jù)庫中的數(shù)據(jù)并且顯示出來
復(fù)制代碼 代碼如下:
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ù)
復(fù)制代碼 代碼如下:
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ù)
復(fù)制代碼 代碼如下:
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ù)
復(fù)制代碼 代碼如下:
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();
相關(guān)文章
.NET Core讀取配置文件方式詳細(xì)總結(jié)
這篇文章主要為大家詳細(xì)總結(jié)了.NET Core讀取配置文件方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
利用委托把用戶控件的值顯示于網(wǎng)頁案例應(yīng)用
用戶控件(UserControl)是集成一個(gè)功能,需要處理好的數(shù)據(jù),然后存數(shù)據(jù)庫中并顯示于網(wǎng)頁上,讓用戶能檢測(cè)到處理的數(shù)據(jù)情況,接下來將介紹利用委托把用戶控件的值顯示于網(wǎng)頁上,感興趣的朋友可以了解下2013-02-02
微信公眾平臺(tái)開發(fā)之認(rèn)證"成為開發(fā)者".Net代碼解析
這篇文章主要為大家詳細(xì)解析了微信公眾平臺(tái)開發(fā)之認(rèn)證"成為開發(fā)者".Net代碼,感興趣的小伙伴們可以參考一下2016-06-06
ASP.NET 鏈接 Access 數(shù)據(jù)庫路徑問題最終解決方案
ASP.NET 鏈接 Access 數(shù)據(jù)庫路徑問題最終解決方案...2007-04-04
vs2010出現(xiàn)error MSB8008的解決方法
這篇文章主要為大家詳細(xì)介紹了vs2010問題error MSB8008: 指定的平臺(tái)工具集(v110)未安裝或無效的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
用ASP.NET做的個(gè)性化的郵件發(fā)送系統(tǒng)
如果要你用ASP來做一個(gè)郵件發(fā)送系統(tǒng),你一定認(rèn)為這是一個(gè)比較復(fù)雜的工作。其實(shí)也的確是這樣。但當(dāng)他的后繼產(chǎn)品ASP.NET被推出以后,他的強(qiáng)大功能就使的這一切就變的相對(duì)簡單了。真的這樣神奇么?我們就通過ASP.NET做一個(gè)郵件發(fā)送系統(tǒng),看看到底有什么奧秘,是不是真的簡單。2008-02-02
.Net讀取Excel 返回DataTable實(shí)例代碼
這篇文章主要介紹了.Net讀取Excel 返回DataTable實(shí)例代碼,有需要的朋友可以參考一下2014-01-01

