【问题标题】:Connect asp.net(vb) to database in SQL Server将 asp.net(vb) 连接到 SQL Server 中的数据库
【发布时间】:2016-05-21 17:07:27
【问题描述】:

我正在我的 asp.net 页面中创建一个登录表单,我现在的问题是我不知道如何将我的 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>

【问题讨论】:

  • 看看这个MSDN page它可能对你有帮助。

标签: asp.net sql-server vb.net


【解决方案1】:

这可能对你有帮助。根据你的数据更改代码

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

我希望你可以在你的服务器标签中嵌入相同的内容。你可以参考以下链接进行澄清

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

【讨论】:

  • 检查你的机器上是否安装了sqlserver?连接是否正常?
  • 谢谢..我已经调试了你给我的代码..让它正常工作。
  • 是 vb.net 的新手,你的问题帮助我提高了知识。:)
【解决方案2】:
String cons = "Data Source=.; initial Catalog=SalaamUnExamSystem; Integrated Security=true";
            SqlConnection con = new SqlConnection(cons);
            SqlCommand cmd = new SqlCommand("insert into stdtbl values('" + StdName.Text + "','" + FName.Text + "','"+gender+"')",con);
            con.Open();
            int r=cmd.ExecuteNonQuery();
            if (r > 0)
            {
                lblMessage.Text="inserted";
            }
            else
            {
                lblMessage.Text = "Not Inserted";
            }

【讨论】:

  • 用于连接两方
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 2022-11-26
  • 2018-07-07
  • 1970-01-01
  • 2016-08-16
相关资源
最近更新 更多