C#登录系统到SQLserver? [英] C# loginsystem to SQLserver?

查看:71
本文介绍了C#登录系统到SQLserver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这件事上有很大的问题.

我使用C#创建数据库,但我不知道如何将其连接到SQL Server.

have a BIG problem with this matter.

i make a database using C# and i don''t know how to connect it to SQL server.

<pre lang="xml"><pre lang="xml"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!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">
    <title>Login</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="DIV1">
        <asp:Login ID="Login1" runat="server" BackColor="White" BorderColor="Blue" BorderStyle="Solid"
            BorderWidth="1px" CreateUserText="Create New User" CreateUserUrl="~/create.aspx"
            DestinationPageUrl="front.aspx" Font-Names="Verdana" Font-Size="10pt" PasswordRecoveryText="Forgot Password?"
            PasswordRecoveryUrl="forgotpass.aspx" OnAuthenticate="Login1_Authenticate">
            <TitleTextStyle BackColor="Blue" Font-Bold="True" ForeColor="White" />
        </asp:Login>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestSQLConnectionString %>"
            SelectCommand="SELECT * FROM [Admin]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>







using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            String strConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("DBAuth.mdb") + ";";
            OleDbConnection Conn = new OleDbConnection(strConn);            Conn.Open();
            String strSQL = "SELECT Pwd FROM Tbl_MA_Users WHERE Email = ''" + txtEmail.Text + "''";
            OleDbCommand Cmd = new OleDbCommand(strSQL, Conn);
            //Create a datareader, connection object
            OleDbDataReader Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            //Get the first row and check the password.
            if (Dr.Read())
            {
                if (Dr["Pwd"].ToString() == txtPwd.Text)
                    FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);
                else
                    lblLoginMsg.Text = "Invalid password.";
            }
            else
                lblLoginMsg.Text = "Login name not found.";
            Dr.Close();
        }
 
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
    }
}




而且我在这方面有问题.




and i have a problem in this line.

String strSQL = "SELECT Pwd FROM Tbl_MA_Users WHERE Email = ''" + txtEmail.Text + "''";



我希望您能提供帮助.



i hope you can help.

推荐答案

ConnectionStrings:TestSQLConnectionString%> SelectCommand ="SELECT * FROM [Admin]"></asp:SqlDataSource> </div> </form> </body> </html>
ConnectionStrings:TestSQLConnectionString %>" SelectCommand="SELECT * FROM [Admin]"></asp:SqlDataSource> </div> </form> </body> </html>







using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            String strConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("DBAuth.mdb") + ";";
            OleDbConnection Conn = new OleDbConnection(strConn);            Conn.Open();
            String strSQL = "SELECT Pwd FROM Tbl_MA_Users WHERE Email = ''" + txtEmail.Text + "''";
            OleDbCommand Cmd = new OleDbCommand(strSQL, Conn);
            //Create a datareader, connection object
            OleDbDataReader Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            //Get the first row and check the password.
            if (Dr.Read())
            {
                if (Dr["Pwd"].ToString() == txtPwd.Text)
                    FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);
                else
                    lblLoginMsg.Text = "Invalid password.";
            }
            else
                lblLoginMsg.Text = "Login name not found.";
            Dr.Close();
        }
 
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
    }
}




而且我在这方面有问题.




and i have a problem in this line.

String strSQL = "SELECT Pwd FROM Tbl_MA_Users WHERE Email = ''" + txtEmail.Text + "''";



我希望您能提供帮助.



i hope you can help.


连接字符串是为MSAccess而不是为Sql Server设置的,因此这可能是未连接的原因.
The connection string is set up for MSAccess not for Sql Server, so this might could be the reason for not connecting.


要从C#.NET连接到SQL Server,您需要创建一个如下的连接字符串:

To connect to SQL Server from C#.NET, you need to create a connection string such as below:

private SqlConnection connection;
private string connectionString =
@"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );



接下来,使用上面创建的



Next, you use the

SqlConnection

对象创建"SqlCommand",如下所示:

object created above to create a ''SqlCommand'', as shown below:

SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid", connection); 



此处显示的SQL查询可以由SELECT,INSERT,UPDATE查询等代替.

接下来要在数据库中执行SQL查询,请使用以下方法:
ExecuteReader-执行SELECT查询
ExecuteNonQuery-执行INSERT,DELETE,UPDATE和SET语句.

这是关于如何从C#连接到SQL Server数据库以及如何在数据库中执行SQL查询的简短描述.
有关连接字符串,方法及其参数的详细信息,请检查以下链接:(



The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.

Next to execute the SQL queries in the database, you use the following methods:
ExecuteReader - to execute SELECT queries
ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database.
For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html )
Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.


这篇关于C#登录系统到SQLserver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆