带有postgresql数据库的

时间:2016-05-05 05:46:53

标签: c# asp.net postgresql

我是postgres和asp.net的新手。我试图使用Npgsql在VS 2015中创建一个简单的登录页面。我不知道如何与c#和postgres数据库建立联系。如果有人能帮助我摆脱错误的地方。提前谢谢。

我的Login.aspx代码如下所示:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="LoginPortal.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="css/bootstrap.css" rel="stylesheet" />
    <link href="css/my-styles.css" rel="stylesheet" media="screen" />
    <link href="dist/css/bootstrap.min.css" rel="stylesheet" media="screen" />
    <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome,firefox=1' /> 
     <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css" /> 
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <link href="client/fine-uploader-new.css" rel="stylesheet" />
    <script src="client/jquery.fine-uploader.js"></script>    
    <link rel="stylesheet" href="http://bootstrapformhelpers.com/assets/css/bootstrap-formhelpers.min.css" media="screen" />
    <script src="http://bootstrapformhelpers.com/assets/js/bootstrap-formhelpers.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="http://bootstrapformhelpers.com/assets/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/dropzone/3.8.4/css/dropzone.css"></script>
  <script type="text/javascript" src="js/bootstrap-filestyle.min.js"> </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div class="container" style="margin-left: 40px">
        <div class="panel panel-primary panel-transparent">
        <div class="panel-heading">Login Page</div>
        <div class="panel-body">
        <asp:Label ID="lblUsername" runat="server" Text="Username"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>&nbsp;&nbsp;
        <asp:RequiredFieldValidator id="userValidator" runat="server"
            controltovalidate="txtUserName" errormessage="Please enter a valid Username!"></asp:RequiredFieldValidator>
        <br />
        <br/>
        <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>&nbsp;&nbsp;<asp:RequiredFieldValidator id="passwordValidator"
            runat="server" controltovalidate="txtPassword" errormessage="Please enter your Password!"></asp:RequiredFieldValidator>
        <br /><br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnLogin" runat="server" Text="Login" onclick="btnlogin_Click" />
        &nbsp;&nbsp;&nbsp;
            <asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" Text="Cancel" />
        <br /><br /><br />

     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="lblMsg" runat="server"></asp:Label>

     </div>
    </div>

    </div>
    </form>
</body>
</html>

我尝试的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Npgsql; 

namespace LoginPortal
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            string ConString = String.Format("Server=localhost;Port=5432;User Id=postgres;password=Att1234$$;Database=LoginCredits");
            try
            {
                NpgsqlConnection con = new NpgsqlConnection(ConString);
                con.Open();
                string sql = "Select * from logintab where username='" + txtUserName.Text + "' and password ='" + txtPassword.Text + "'";
                NpgsqlCommand cmd = new NpgsqlCommand(sql,con);
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                //NpgsqlDataReader rd = cmd.ExecuteReader();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    Session["id"] = txtUserName.Text;
                    Response.Redirect("Home.aspx");
                    Response.Write("Login Suucessfull!");
                    //Session.RemoveAll();
                }
                else
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = "Please enter a Valid Username and Password";
                }
            }
            catch(NpgsqlException ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            lblMsg.Visible = false;
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtUserName.Focus();
        }
    }
}

我还没有完成代码。请建议我如何建立与数据库的连接。

0 个答案:

没有答案