2016-02-11 61 views
1

我在我的asp.net頁面中創建登錄表單,現在我的問題是我不知道如何將我的asp.net(vb)連接到SQL Server數據庫。將asp.net(vb)連接到SQL Server中的數據庫

我想插在

<script runat="server"> </script> 

連接代碼這可能嗎?

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="_Default" %> 
<script runat="server"> 
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 
     lblDate.Text = Format(Now, "MMMM dd,yyyy hh:mmtt") 
    End Sub 
</script> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <link rel="icon" type="image/png" href="SENCOR_Logo.ico"> 
    <title>Auto OCS System</title> 
    <link rel="stylesheet" type="text/css" href="style.css"> 

</head> 
<body> 
    <div class="date_time"> 
    <p><asp:Label id="lblDate" runat="server" Text ="DateandTime"></asp:Label></p> 
    </div> 
    <div id="wrapper"> 
    <div class = "container"> 
     <div class = "login"> 
      <h1><img src="SENCOR_Logo.jpg" height="35px;"></h1> 
      <form method = "post" action = "" runat="server"> 
       <p><asp:TextBox id="txtUser" runat="server" placeholder="Username"/></p> 
       <p><asp:TextBox id="txtPass" runat="server" TextMode="Password" placeholder="Password"/></p> 
       <p class = "remember_me"> 
       <label> 
       <label> 
        <asp:CheckBox id="chkRem" runat="server"/> 
        Remember me on this computer 
       </label> 
       </label> 
       </p> 
        <p class="submit"><asp:Button id="btnLogin" Text="Login" runat="server" OnClick = "btnLogin_Click" /></p> 
      </form> 
     </div> 
     <div class="login-help"> 
      <p>Forget your password? <asp:HyperLink id="lnkRem" runat="server" Text="Click here to reset it." NavigateUrl ="~/Login.aspx"></asp:HyperLink></p> 
     </div> 
    </div></div> 
    <div class="footer"> 
     <p>&copy; 2016 Emilyn Pascua. All rights reserved.</p> 
    </div> 
</body> 
</html> 
+0

請參閱此[MSDN頁面](https://msdn.microsoft.com/en-us/library/jj653752(v = vs.110).aspx)它可能會對您有所幫助。 – Yog

回答

1

這可以根據你的數據幫助you.change代碼

Private Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click 
    ConnectToSQL() 

End Sub 

Private Sub ConnectToSQL() 

    Dim con As New SqlConnection 
    Dim cmd As New SqlCommand 
    Dim Passowrd As String 
    Dim Passowrd2 As String 
    Dim userName As String 

    Try 
     If 
      /*change the data source and initial catalog according to your sql server engine and data base*/ 
      con.ConnectionString = "Data Source = YOUR-PC; Initial Catalog = YOUR-DB; Integrated Security = True" 
      con.Open() 

      cmd.Connection = con 
      /*change the data fields names and table according to your database*/ 
      cmd.CommandText = " SELECT UserName, Password FROM AdminDetail WHERE (UserName = '" & txtUsername.Text & "') AND (Password = '" & txtPassword.Text & "')" 

      Dim lrd As SqlDataReader = cmd.ExecuteReader() 
      If lrd.HasRows Then 
       While lrd.Read() 

        //Do something here 
        Passowrd = lrd("Password").ToString() 
        userName = lrd("UserName").ToString() 

        Passowrd2 = txtPassword.Text() 

        If Passowrd = Passowrd2 And userName = txtUsername.Text Then 

         MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information 
             ) 
         frmMain.Show() 
         Me.Hide() 

         //Clear all fields 
         txtPassword.Text = "" 
         txtUsername.Text = "" 

        End If 

       End While 

      Else 
       MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
       //Clear all fields 
       txtPassword.Text = "" 
       txtUsername.Text = "" 
      End If 

     End If 

    Catch ex As Exception 
     MessageBox.Show("Error while connecting to SQL Server." & ex.Message) 

    Finally 
     con.Close() //Whether there is error or not. Close the connection. 

    End Try 

End Sub 

我希望你可以嵌入在你的服務器的同一tags..you可參考澄清

以下鏈接http://www.aspsnippets.com/Articles/Different-Embedded-Code-Blocks-and-its-use-in-ASPNet.aspx

+0

謝謝..我會試試這個 – Emz1402

+0

它總是說錯誤在連接sql – Emz1402

+0

檢查是sqlserver安裝在你的機器上?並且連接正常工作? – Yog