我在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>© 2016 Emilyn Pascua. All rights reserved.</p>
</div>
</body>
</html>
答案 0 :(得分: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
答案 1 :(得分:0)
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";
}