Click here to Skip to main content
15,906,094 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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.
Posted
Comments
R. Giskard Reventlov 27-Apr-11 3:23am    
You need to say what the problem is. Also, looks like you're using an Access database (the connection string is set up for Access) not Sql Server.
Ankit Rajput 27-Apr-11 3:26am    
He has asked it third time.
chaosgray 27-Apr-11 3:29am    
how can connect it from the sql server? pls. help me.

The connection string is set up for MSAccess not for Sql Server, so this might could be the reason for not connecting.
 
Share this answer
 
Comments
chaosgray 27-Apr-11 3:27am    
so, how can i connect it from the sql server? pls. help me.
RDBurmon 27-Apr-11 4:03am    
Search this on google "Connection string for 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
object created above to create a 'SqlCommand', as shown below:
SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid", connection); 


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.
 
Share this answer
 
v3
Hi,

Please see the connection string from this site.
Connection Strings

Regards
AR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900