1.創(chuàng)建好項(xiàng)目后在我們的Web.config里面連接SQL Server數(shù)據(jù)庫
?2.寫入代碼
<appSettings> <add key="conStr" value="Data Source=localhost;Initial Catalog=s;Integrated Security=True;" /> </appSettings>
conStr是字段名字,后面連接需要,localhost是本地地址的意思,s是數(shù)據(jù)庫中的表名。
3.選中shujuku練習(xí)右擊,選擇添加,然后在選擇Web窗體
?
4.創(chuàng)建好的Web窗體如下
窗體中的代碼:
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
就添加了一個顯示控件
cs文件代碼如下:
using System; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace shujuiku練習(xí) { public partial class WebForm1 : System.Web.UI.Page { private static string constr;//這里就是我們定義數(shù)據(jù)庫的字段constr private static SqlConnection conn;//創(chuàng)建數(shù)據(jù)庫連接字段 protected void Page_Load(object sender, EventArgs e) { constr = ConfigurationManager.AppSettings["conStr"].ToString(); conn = new SqlConnection(constr);//創(chuàng)建數(shù)據(jù)庫連接實(shí)例化對象 DataSet ds = new DataSet();// 實(shí)例化DataSet類型的對象ds SqlDataAdapter s = new SqlDataAdapter("select * from SysUser;", conn);//實(shí)例化DataAdapter對象 s.Fill(ds);//填充ds GridView1.DataSource=ds;//將該數(shù)據(jù)顯示在GridView1控件上 GridView1.DataBind(); } } }
5.網(wǎng)頁效果圖和數(shù)據(jù)庫的表:
?文章來源地址http://www.zghlxwxcb.cn/news/detail-400909.html
文章來源:http://www.zghlxwxcb.cn/news/detail-400909.html
?
到了這里,關(guān)于ASP.NET連接數(shù)據(jù)庫(SQL Server)的操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!