标签:des style blog http color io os 使用 ar
目的:创建与数据库交互的asp.net例子
一、新建一个空解决方案(也可直接建项目)
二、在项目中新建一个项目,然后新建三层 分别是:DAL 、BLL 、Model
三、创建数据库(我使用vs2010自带数据库 感觉比较方便)
首先第一次使用在 服务器资源管理器中 要建立一个数据库 在空白处 右键 即可建立
然后插入数据
注意:红框 连接数据库的时候 会需要字符串 点击红框 你懂得!!
四、DBHelper编写(网上资料) DBHelper 写在DAL中
1 Imports System 2 Imports System.Collections 3 Imports System.Collections.Specialized 4 Imports System.Data 5 Imports System.Data.SqlClient 6 Imports System.Configuration 7 Imports System.Data.Common 8 Imports System.Collections.Generic 9 10 ‘Namespace Tianli.DBUtility 11 12 ‘修改成實際項目的命名空間名稱 13 ‘‘‘ <summary> 14 ‘‘‘ 資料訪問基礎類(基於Oracle) 15 ‘‘‘ 可以用戶可以修改滿足自己專案的需要。 16 ‘‘‘ </summary> 17 Public MustInherit Class DBHelper 18 19 ‘ Protected Shared connectionString As String = ConfigurationSettings.AppSettings("conectiongstring") 20 Protected Shared connectionString As String = "Data Source=WISEPERSON-2006\SQLEXPRESS;Initial Catalog=tom;Integrated Security=True;Pooling=False" 21 22 Public Sub New() 23 End Sub 24 25 #Region "公用方法" 26 27 Public Shared Function Exists(ByVal strSql As String, ByVal ParamArray cmdParms As SqlParameter()) As Boolean 28 Dim obj As Object = Getsingle(strSql, cmdParms) 29 Dim cmdresult As Integer 30 If ([Object].Equals(obj, Nothing)) OrElse ([Object].Equals(obj, System.DBNull.Value)) Then 31 cmdresult = 0 32 Else 33 cmdresult = Integer.Parse(obj.ToString()) 34 End If 35 If cmdresult = 0 Then 36 Return False 37 Else 38 Return True 39 End If 40 End Function 41 Public Shared Function Exists(ByVal strSql As String) As Boolean 42 Dim obj As Object = Getsingle(strSql) 43 Dim cmdresult As Integer 44 If ([Object].Equals(obj, Nothing)) OrElse ([Object].Equals(obj, System.DBNull.Value)) Then 45 cmdresult = 0 46 Else 47 cmdresult = Integer.Parse(obj.ToString()) 48 End If 49 If cmdresult = 0 Then 50 Return False 51 Else 52 Return True 53 End If 54 End Function 55 #End Region 56 57 #Region "執行簡單SQL語句" 58 ‘‘‘ <summary> 59 ‘‘‘ 执行sql语句,返回影响的记录数 60 ‘‘‘ </summary> 61 ‘‘‘ <param name="Sqlstring">sql语句</param> 62 ‘‘‘ <returns>影响记录数据</returns> 63 ‘‘‘ <remarks></remarks> 64 Public Shared Function ExecuteSql(ByVal Sqlstring As String) As Integer 65 Dim connection As New SqlConnection(connectionString) 66 If connection.State = ConnectionState.Closed Then 67 connection.Open() 68 End If 69 Dim trans As SqlTransaction = connection.BeginTransaction 70 Dim cmd As New SqlCommand 71 cmd.Connection = connection 72 cmd.Transaction = trans 73 cmd.CommandText = Sqlstring 74 Try 75 Dim row As Integer = cmd.ExecuteNonQuery 76 trans.Commit() 77 Return row 78 Catch ex As Exception 79 trans.Rollback() 80 Throw New Exception(ex.Message) 81 End Try 82 83 End Function 84 85 86 Public Shared Sub ExecuteSqltran(ByVal sqlstringList As ArrayList) 87 Dim conn As New SqlConnection(connectionString) 88 conn.Open() 89 If conn.State = ConnectionState.Closed Then 90 conn.Open() 91 End If 92 Dim trans As SqlTransaction = conn.BeginTransaction 93 Dim cmd As New SqlCommand 94 cmd.Connection = conn 95 cmd.Transaction = trans 96 Try 97 For n As Integer = 0 To sqlstringList.Count - 1 98 Dim strsql As String = sqlstringList(n).ToString() 99 If strsql.Trim().Length > 1 Then 100 cmd.CommandText = strsql 101 cmd.ExecuteNonQuery() 102 End If 103 Next 104 trans.Commit() 105 Catch ex As Exception 106 trans.Rollback() 107 Throw New Exception(ex.Message) 108 End Try 109 End Sub 110 111 ‘‘‘ <summary> 112 ‘‘‘ 執行一條計算查詢結果語句,返回查詢結果(object)。 113 ‘‘‘ </summary> 114 ‘‘‘ <param name="SQLString">計算查詢結果語句</param> 115 ‘‘‘ <returns>查詢結果(object)</returns> 116 Public Shared Function Getsingle(ByVal sqlstring) As Object 117 Dim connection As New SqlConnection(connectionString) 118 If connection.State = ConnectionState.Closed Then 119 connection.Open() 120 End If 121 Dim cmd As New SqlCommand(sqlstring, connection) 122 Try 123 Dim obj As Object = cmd.ExecuteScalar() 124 If ([Object].Equals(obj, Nothing)) Or ([Object].Equals(obj, System.DBNull.Value)) Then 125 Return Nothing 126 Else 127 Return obj 128 End If 129 Catch ex As Exception 130 connection.Close() 131 Throw New Exception(ex.Message) 132 End Try 133 134 End Function 135 136 ‘‘‘ <summary> 137 ‘‘‘ 執行查詢語句,返回SqlExecuteReader 138 ‘‘‘ </summary> 139 ‘‘‘ <param name="strSQL">查詢語句</param> 一般用於聚集函數返回值 140 ‘‘‘ <returns>SqlExecuteReader</returns> 141 Public Shared Function SqlExecuteReader(ByVal strSQL As String) As SqlDataReader 142 Dim connection As New SqlConnection(connectionString) 143 If connection.State = ConnectionState.Closed Then 144 connection.Open() 145 End If 146 Dim cmd As New SqlCommand(strSQL, connection) 147 Try 148 Dim myReader As SqlDataReader = cmd.ExecuteReader() 149 Return myReader 150 Catch ex As Exception 151 Throw New Exception(ex.Message) 152 End Try 153 End Function 154 155 ‘‘‘ <summary> 156 ‘‘‘ 執行帶一個存儲過程參數的的SQL語句。 157 ‘‘‘ </summary> 158 ‘‘‘ <param name="SQLString">SQL語句</param> 159 ‘‘‘ <param name="content">參數內容,比如一個欄位是格式複雜的文章,有特殊符號,可以通過這個方式添加</param> 160 ‘‘‘ <returns>影響的記錄數</returns> 161 162 Public Shared Function ExecuteSql(ByVal SQLString As String, ByVal content As String) As Integer 163 Dim connection As New SqlConnection(connectionString) 164 Dim cmd As New SqlCommand(SQLString, connection) 165 Dim myParameter As New System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText) 166 myParameter.Value = content 167 cmd.Parameters.Add(myParameter) 168 Try 169 connection.Open() 170 Dim rows As Integer = cmd.ExecuteNonQuery() 171 Return rows 172 Catch E As System.Data.SqlClient.SqlException 173 Throw New Exception(E.Message) 174 Finally 175 cmd.Dispose() 176 connection.Close() 177 End Try 178 End Function 179 180 ‘‘‘ <summary> 181 ‘‘‘ 返回最大值 182 ‘‘‘ </summary> 183 ‘‘‘ <param name="FieldName">字段名称</param> 184 ‘‘‘ <param name="TableName">表名称</param> 185 ‘‘‘ <returns></returns> 186 ‘‘‘ <remarks></remarks> 187 Public Shared Function GetMaxID(ByVal FieldName As String, ByVal TableName As String) As Integer 188 Dim strsql As String = "select max(" + FieldName + ")+1 from " + TableName 189 Dim obj As Object = Getsingle(strsql) 190 If obj Is Nothing Then 191 Return 1 192 Else 193 Return Integer.Parse(obj.ToString()) 194 End If 195 End Function 196 197 ‘ <summary> 198 ‘ 向資料庫裏插入圖像格式的欄位(和上面情況類似的另一種實例) 199 ‘ </summary> 200 ‘ <param name="strSQL">SQL語句</param> 201 ‘ <param name="fs">圖像位元組,資料庫的欄位類型為image的情況</param> 202 ‘ <returns>影響的記錄數</returns> 203 204 ‘//保存SQL server2000的Image 205 ‘string pathName; 206 ‘pathName = this.openFileDialog1.FileName; 207 ‘//將圖像讀入到字節數組 208 ‘System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read); 209 ‘byte[] buffByte = new byte[fs.Length]; 210 ‘fs.Read(buffByte,0,(int)fs.Length); 211 ‘fs.Close(); 212 ‘fs = null; 213 214 ‘public byte[] ConvertImage(Image image) 215 ‘{ 216 ‘ FileStream fs=new FileStream("imagetemp",FileMode.Create,FileAccess.Write,FileShare.None); 217 ‘ BinaryFormatter bf = new BinaryFormatter(); 218 ‘ bf.Serialize(fs,(object)image); 219 ‘ fs.Close(); 220 ‘ fs=new FileStream("imagetemp",FileMode.Open,FileAccess.Read,FileShare.None); 221 ‘ byte[] bytes = new byte[fs.Length]; 222 ‘ fs.Read(bytes,0,(int)fs.Length); 223 ‘ fs.Close(); 224 ‘ return bytes; 225 ‘} 226 227 ‘public Image ReadImage(byte[] bytes) 228 ‘{ 229 ‘ FileStream fs=new FileStream("imagetemp1",FileMode.Create,FileAccess.Write,FileShare.None); 230 ‘ foreach(byte a in bytes) 231 ‘ { 232 ‘ fs.WriteByte(a); 233 ‘ } 234 ‘ fs.Close(); 235 ‘ fs=new FileStream("imagetemp1",FileMode.Open,FileAccess.Read,FileShare.None); 236 ‘ BinaryFormatter bf = new BinaryFormatter(); 237 ‘ object obj=bf.Deserialize(fs); 238 ‘ fs.Close(); 239 ‘ return (Image)obj; 240 ‘} 241 Public Shared Function ExecuteSqlInsertImg(ByVal strSQL As String, ByVal fs As Byte()) As Integer 242 Dim connection As New SqlConnection(connectionString) 243 If connection.State = ConnectionState.Closed Then 244 connection.Open() 245 End If 246 Dim cmd As New SqlCommand(strSQL, connection) 247 Dim myParameter As New SqlParameter("@fs", SqlDbType.Image) 248 myParameter.Value = fs 249 cmd.Parameters.Add(myParameter) 250 Try 251 Dim rows As Integer = cmd.ExecuteNonQuery 252 Return rows 253 Catch ex As Exception 254 Throw New Exception(ex.Message) 255 Finally 256 cmd.Dispose() 257 connection.Close() 258 End Try 259 End Function 260 261 262 ‘‘‘ <summary> 263 ‘‘‘ 執行查詢語句,返回DataSet 264 ‘‘‘ </summary> 265 ‘‘‘ <param name="SQLString">查詢語句</param> 266 ‘‘‘ <returns>DataSet</returns> 267 268 Public Shared Function Query(ByVal SQLString As String) As DataSet 269 Dim connection As New SqlConnection(connectionString) 270 Dim ds As New DataSet 271 Try 272 connection.Open() 273 Dim myda As New SqlDataAdapter(SQLString, connection) 274 myda.Fill(ds) 275 Catch ex As Exception 276 Throw New Exception(ex.Message) 277 End Try 278 Return ds 279 End Function 280 281 #End Region 282 283 #Region "消息模塊" 284 ‘Public Shared Function Show(ByVal page As System.Web.UI.Page, ByVal msg As String) 285 ‘ page.RegisterStartupScript("message", "<script language=‘javascript‘ defer>alert(‘" + msg.ToString() + "‘);</script>") 286 ‘End Function 287 #End Region 288 289 #Region "執行帶參數的SQL語句" 290 291 Private Shared Sub PrepareCommand(ByRef cmd As SqlCommand, ByRef conn As SqlConnection, ByRef trans As SqlTransaction, ByRef cmdText As String, ByRef cmdParms As SqlParameter()) 292 If conn.State = ConnectionState.Closed Then 293 conn.Open() 294 End If 295 cmd.Connection = conn 296 cmd.CommandText = cmdText 297 If Not trans Is Nothing Then 298 cmd.Transaction = trans 299 End If 300 301 cmd.CommandType = CommandType.Text 302 If Not cmdParms Is Nothing Then 303 For Each parm As SqlParameter In cmdParms 304 cmd.Parameters.Add(parm) 305 Next 306 End If 307 End Sub 308 309 Public Shared Function ExecuteSql(ByVal SQLString As String, ByVal ParamArray cmdParms As SqlParameter()) As Integer 310 Dim connection As New SqlConnection(connectionString) 311 If connection.State = ConnectionState.Closed Then 312 connection.Open() 313 End If 314 Dim trans As SqlTransaction = connection.BeginTransaction 315 Dim cmd As New SqlCommand 316 Try 317 PrepareCommand(cmd, connection, trans, SQLString, cmdParms) 318 Dim rows As Integer = cmd.ExecuteNonQuery() 319 cmd.Parameters.Clear() 320 trans.Commit() 321 Return rows 322 Catch ex As Exception 323 trans.Rollback() 324 Throw New Exception(ex.Message) 325 Finally 326 connection.Close() 327 cmd.Dispose() 328 End Try 329 End Function 330 331 332 ‘* 333 ‘ Dim parameters As OracleParameter() = {New OracleParameter("DPPM_", OracleType.Number), New OracleParameter("Issue_id_", OracleType.Number), New OracleParameter("code1_id_", OracleType.Number), New OracleParameter("defectcode_id_", OracleType.Number)} 334 ‘ parameters(0).Value = dppm 335 ‘ parameters(1).Value = issue_id 336 ‘ parameters(2).Value = code1_id 337 ‘ parameters(3).Value = defect_code_id 338 ‘ Return ITSC.DbHelperSQL.ExecuteSP("Updatecsm_issue_code", parameters) 339 ‘*‘ 340 Private Shared Sub PrepareCommandSP(ByVal cmd As SqlCommand, ByVal conn As SqlConnection, ByVal trans As SqlTransaction, ByVal cmdText As String, ByVal cmdParms As SqlParameter()) 341 If conn.State <> ConnectionState.Open Then 342 conn.Open() 343 End If 344 cmd.Connection = conn 345 If Not trans Is Nothing Then 346 cmd.Transaction = trans 347 End If 348 cmd.CommandText = cmdText 349 cmd.CommandType = CommandType.StoredProcedure 350 If Not cmdParms Is Nothing Then 351 For Each parm As SqlParameter In cmdParms 352 cmd.Parameters.Add(parm) 353 Next 354 End If 355 End Sub 356 357 358 Public Shared Function ExecuteSP(ByVal SP As String, ByVal ParamArray cmdParms As SqlParameter()) As Integer 359 Dim connection As New SqlConnection(connectionString) 360 If connection.State = ConnectionState.Closed Then 361 connection.Open() 362 End If 363 Dim trans As SqlTransaction = connection.BeginTransaction() 364 Dim cmd As New SqlCommand 365 Try 366 PrepareCommandSP(cmd, connection, trans, SP, cmdParms) 367 Dim rows As Integer = cmd.ExecuteNonQuery() 368 cmd.Parameters.Clear() 369 trans.Commit() 370 Return rows 371 Catch ex As System.Data.SqlClient.SqlException 372 trans.Rollback() 373 Throw New Exception(ex.Message) 374 End Try 375 End Function 376 377 378 ‘‘‘ <summary> 379 ‘‘‘ 執行一條計算查詢結果語句,返回查詢結果(object)。 380 ‘‘‘ </summary> 381 ‘‘‘ <param name="SQLString">計算查詢結果語句</param> 382 ‘‘‘ <returns>查詢結果(object)</returns> 383 Public Shared Function GetSingle(ByVal SQLString As String, ByVal ParamArray cmdParms As SqlParameter()) As Object 384 Dim connection As New SqlConnection(connectionString) 385 Dim cmd As New SqlCommand 386 Try 387 PrepareCommand(cmd, connection, Nothing, SQLString, cmdParms) 388 Dim obj As Object = cmd.ExecuteScalar() 389 cmd.Parameters.Clear() 390 If ([Object].Equals(obj, Nothing)) OrElse ([Object].Equals(obj, System.DBNull.Value)) Then 391 Return Nothing 392 Else 393 Return obj 394 End If 395 Catch e As System.Data.SqlClient.SqlException 396 Throw New Exception(e.Message) 397 End Try 398 End Function 399 400 ‘‘‘ <summary> 401 ‘‘‘ 执行查询语句,返回DataReader 402 ‘‘‘ </summary> 403 ‘‘‘ <param name="SQLString">查询语句</param> 404 ‘‘‘ <param name="cmdParms">查询参数</param> 405 ‘‘‘ <returns>DataReader</returns> 406 ‘‘‘ <remarks>DataReader</remarks> 407 Public Shared Function ExecuteReader(ByVal SQLString As String, ByVal ParamArray cmdParms As SqlParameter()) As SqlDataReader 408 Dim connection As New SqlConnection(connectionString) 409 Dim cmd As New SqlCommand 410 Try 411 PrepareCommand(cmd, connection, Nothing, SQLString, cmdParms) 412 Dim myReader As SqlDataReader = cmd.ExecuteReader() 413 cmd.Parameters.Clear() 414 Return myReader 415 Catch e As System.Data.SqlClient.SqlException 416 Throw New Exception(e.Message) 417 End Try 418 End Function 419 420 421 ‘‘‘ <summary> 422 ‘‘‘ 執行查詢語句,返回DataSet 423 ‘‘‘ </summary> 424 ‘‘‘ <param name="SQLString">查詢語句</param> 425 ‘‘‘ <returns>DataSet</returns> 426 Public Shared Function Query(ByVal SQLString As String, ByVal ParamArray cmdParms As SqlParameter()) As DataSet 427 Dim connection As New SqlConnection(connectionString) 428 Dim cmd As New SqlCommand 429 PrepareCommand(cmd, connection, Nothing, SQLString, cmdParms) 430 Dim Myda As New SqlDataAdapter(cmd) 431 Dim ds As New DataSet 432 Try 433 Myda.Fill(ds, "ds") 434 cmd.Parameters.Clear() 435 Catch ex As System.Data.SqlClient.SqlException 436 Throw New Exception(ex.Message) 437 End Try 438 Return ds 439 End Function 440 441 ‘‘‘ <summary> 442 ‘‘‘ 執行多條SQL語句,實現資料庫事務。 443 ‘‘‘ </summary> 444 ‘‘‘ <param name="SQLStringList">SQL語句的哈希表(key為sql語句,value是該語句的OracleParameter[])</param> 445 Public Shared Sub ExecuteSqlTran(ByVal SQLStringList As Hashtable) 446 Dim conn As New SqlConnection(connectionString) 447 conn.Open() 448 Dim trans As SqlTransaction = conn.BeginTransaction() 449 Dim cmd As New SqlCommand 450 Try 451 ‘迴圈 452 For Each myDE As DictionaryEntry In SQLStringList 453 Dim cmdText As String = myDE.Key.ToString() 454 Dim cmdParms As SqlParameter() = DirectCast(myDE.Value, SqlParameter()) 455 PrepareCommand(cmd, conn, trans, cmdText, cmdParms) 456 Dim val As Integer = cmd.ExecuteNonQuery() 457 cmd.Parameters.Clear() 458 trans.Commit() 459 Next 460 Catch 461 trans.Rollback() 462 Throw 463 End Try 464 End Sub 465 #End Region 466 467 #Region "存儲過程操作" 468 469 ‘‘‘ <summary> 470 ‘‘‘ 執行存儲過程 471 ‘‘‘ </summary> 472 ‘‘‘ <param name="storedProcName">存儲過程名</param> 473 ‘‘‘ <param name="parameters">存儲過程參數</param> 474 ‘‘‘ <returns>OracleDataReader</returns> 475 Public Shared Function RunProcedure(ByVal storedProcName As String, ByVal parameters As IDataParameter()) As SqlDataReader 476 Dim connection As New SqlConnection(connectionString) 477 Dim returnReader As SqlDataReader 478 connection.Open() 479 Dim command As SqlCommand = BuildQueryCommand(connection, storedProcName, parameters) 480 command.CommandType = CommandType.StoredProcedure 481 returnReader = command.ExecuteReader() 482 Return returnReader 483 End Function 484 485 ‘‘‘ <summary> 486 ‘‘‘ 執行存儲過程 487 ‘‘‘ </summary> 488 ‘‘‘ <param name="storedProcName">存儲過程名</param> 489 ‘‘‘ <param name="parameters">存儲過程參數</param> 490 ‘‘‘ <param name="tableName">DataSet結果中的表名</param> 491 ‘‘‘ <returns>DataSet</returns> 492 Public Shared Function RunProcedure(ByVal storedProcName As String, ByVal parameters As IDataParameter(), ByVal tableName As String) As DataSet 493 Dim connection As New SqlConnection(connectionString) 494 Dim dataSet As New DataSet 495 connection.Open() 496 Dim sqlDA As New SqlDataAdapter 497 sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters) 498 sqlDA.Fill(dataSet, tableName) 499 connection.Close() 500 Return dataSet 501 End Function 502 ‘‘‘ <summary> 503 ‘‘‘ 構建 OracleCommand 物件(用來返回一個結果集,而不是一個整數值) 504 ‘‘‘ </summary> 505 ‘‘‘ <param name="connection">資料庫連接</param> 506 ‘‘‘ <param name="storedProcName">存儲過程名</param> 507 ‘‘‘ <param name="parameters">存儲過程參數</param> 508 ‘‘‘ <returns>OracleCommand</returns> 509 Private Shared Function BuildQueryCommand(ByVal connection As SqlConnection, ByVal storedProcName As String, ByVal parameters As IDataParameter()) As SqlCommand 510 Dim command As New SqlCommand(storedProcName, connection) 511 command.CommandType = CommandType.StoredProcedure 512 For Each parameter As SqlParameter In parameters 513 command.Parameters.Add(parameter) 514 Next 515 Return command 516 End Function 517 518 ‘‘‘ <summary> 519 ‘‘‘ 執行存儲過程,返回影響的行數 520 ‘‘‘ </summary> 521 ‘‘‘ <param name="storedProcName">存儲過程名</param> 522 ‘‘‘ <param name="parameters">存儲過程參數</param> 523 ‘‘‘ <param name="rowsAffected">影響的行數</param> 524 ‘‘‘ <returns></returns> 525 Public Shared Function RunProcedure(ByVal storedProcName As String, ByVal parameters As IDataParameter(), ByRef rowsAffected As Integer) As Integer 526 Dim connection As New SqlConnection(connectionString) 527 Dim result As Integer 528 connection.Open() 529 Dim command As SqlCommand = BuildIntCommand(connection, storedProcName, parameters) 530 rowsAffected = command.ExecuteNonQuery() 531 result = CInt(command.Parameters("ReturnValue").Value) 532 ‘Connection.Close(); 533 Return result 534 End Function 535 536 ‘‘‘ <summary> 537 ‘‘‘ 創建 OracleCommand 物件實例(用來返回一個整數值) 538 ‘‘‘ </summary> 539 ‘‘‘ <param name="storedProcName">存儲過程名</param> 540 ‘‘‘ <param name="parameters">存儲過程參數</param> 541 ‘‘‘ <returns>OracleCommand 物件實例</returns> 542 Private Shared Function BuildIntCommand(ByVal connection As SqlConnection, ByVal storedProcName As String, ByVal parameters As IDataParameter()) As SqlCommand 543 Dim command As SqlCommand = BuildQueryCommand(connection, storedProcName, parameters) 544 command.Parameters.Add(New SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, False, 0, _ 545 0, String.Empty, DataRowVersion.[Default], Nothing)) 546 Return command 547 End Function 548 #End Region 549 End Class 550 ‘End Namespace
五、在BLL中写业务类 stu
1 Public Class stu 2 Function test() As DataSet 3 4 ‘ Dim dt As System.Data.DataTable 5 Dim ds As DataSet 6 ds = DAL.DBHelper.Query("select * from reg") ‘利用dbhelper中的查询方法 查询出结果 给dataSet 7 test = ds ‘ 返回结果 类似return 8 End Function 9 10 End Class
六、创建asp主页 index.aspx (在web.config中设置主页) 拖一个gridview控件用于显示数据
F7进入index.aspx.vb 编辑
1 Public Class index 2 Inherits System.Web.UI.Page 3 4 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 5 Dim st As New BLL.stu ‘注意new 实例化bll中的stu类 6 Dim para As DataSet 7 8 para = st.test() ‘返回dataset结果 类型是dataset 9 GridView1.DataSource = para ‘把结果给gridview 10 GridView1.DataBind() ‘绑定给gridview 11 12 13 End Sub 14 15 End Class
1 <?xml version="1.0" encoding="utf-8"?> 2 3 4 <configuration> <!--<configuration>节根元素,其它节都是在它的内部.--> 5 6 <connectionStrings> 7 <add name="ApplicationServices" 8 connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" 9 providerName="System.Data.SqlClient" /> 10 </connectionStrings> 11 12 <system.web> 13 <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" /> 14 15 <authentication mode="Forms"> 16 <forms loginUrl="~/Account/Login.aspx" timeout="2880" /> 17 </authentication> 18 19 <membership> 20 <providers> 21 <clear/> 22 <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" 23 enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" 24 maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" 25 applicationName="/" /> 26 </providers> 27 </membership> 28 29 <profile> 30 <providers> 31 <clear/> 32 <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> 33 </providers> 34 </profile> 35 36 <roleManager enabled="false"> 37 <providers> 38 <clear/> 39 <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> 40 <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> 41 </providers> 42 </roleManager> 43 44 </system.web> 45 46 47 <!--设置web服务--> 48 <system.webServer> 49 <defaultDocument> 50 <files> 51 <clear /> 52 <add value="index.aspx" /> 53 <!--<add value="Default.htm" /> 54 <add value="Default.asp" /> 55 <add value="index.htm" /> 56 <add value="index.html" />--> 57 </files> 58 </defaultDocument> 59 <modules runAllManagedModulesForAllRequests="true"/> 60 </system.webServer> 61 </configuration>
现在运行
标签:des style blog http color io os 使用 ar
原文地址:http://www.cnblogs.com/Alliumhollandicum/p/4002082.html