Sei sulla pagina 1di 8

ASP.

NET Connection with MS ACCESS DATABASE USING C#

See more:
ASP.NET
Hello Developers!!!!

I want to build a ASP.NET website by using a MS ACCESS database and C# Language.I have
searched a lot but i can't find write answer.
Whenever i try to find connectivity solution, The code that i found on Google search is Like
<pre>
using System.Data.Oledb;

__________________________________
Hide Copy Code
namespace consample
{
pubic class OledbConnection
{
public void Perform()
{
OledbConnection con= new
OledbCOnnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\mydatabase.mdb")//Data provider for MS ACCESS
con.Open();
OLEDBCommand cmd=new OLEDBCOmman();
cmd.Connection=con;
cmd.CommandType=CommandType.Text;
cmd.CommandText="Select * from emp";
OledbDataReader dr= new OledbDataReader();
dr=cmd.Executereader();
while (dr.Read()==true)
{
/// Do some coding here
}
}
}
}

__________________________________________

I am totally confused that why they only give local host or C:\ path if we want to upload our site

1 solution
Solution 1
You can use Server.MapPath() to locate the access file in connectionString so that it searches in
deployed location where application resides. Here main purpose is at runtime application should
be able to locate the access data file.
code snnipet:
Hide Copy Code
OledbConnection con= new
OledbCOnnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="+Server.MapPath("mydatabase.mdb")+";";

Connecting to database(MS Access) in


asp.net c#(hosting website) [closed]
Ask Question
Asked 6 years, 4 months ago
Active 6 years, 4 months ago
Viewed 12k times
0

I have a hosted website, and I need to add a database with password and log-in information. (MS
Access DB), I tried a lot but can't connect with it (on local machine it works). I tried to change
connection string but it still doesn't work. Database is in folder App_Data. Here's what I type in
Login.aspx page:

OleDbConnection con = new OleDbConnection();


con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ Server.MapPath("~\\App_Data\\WebSiteDatabase.accdb");
con.Open();

This does not work. What do I need to change? I've put my web site on somee.com

c# asp.net ms-access
shareimprove this question
edited May 14 '13 at 13:18

Mark Schultheiss
25.7k88 gold badges5757 silver badges8585 bronze badges
asked May 14 '13 at 13:09

user2362291
1222 gold badges33 silver badges77 bronze badges

closed as too localized by Jean-Bernard Pellerin, Crisfole,


MUG4N, Joe Doyle, jthill May 14 '13 at 21:10
This question is unlikely to help any future visitors; it is only relevant to a small geographic area,
a specific moment in time, or an extraordinarily narrow situation that is not generally applicable
to the worldwide audience of the internet. For help making this question more broadly
applicable, visit the help center. If this question can be reworded to fit the rules in the help
center, please edit the question.

 Why are you not using the included SQL Express database? – Jake1164 May 14 '13 at
13:14
 It's a task for education.... – user2362291 May 14 '13 at 13:15
 Is the "program" Access installed on the server? Also, I cringe whenever I see someone
using Access with a website. Please don't do it. – CM Kanode May 14 '13 at 13:15
 Yes it's intalled – user2362291 May 14 '13 at 13:16
 somee.com/DOKA/DoHelpTopics.aspx?docode=false&thnid=91 they have this but I
don't understand where I should write it – user2362291 May 14 '13 at 13:18

show 1 more comment

1 Answer
active oldest votes
3

Did you check the help page on SOMEE for connecting to an access database?

QUOTE FROM Somee.com help:

Connect to MS access database usin DSN-less connection


Doka only provides DSN-less connection to the Access databases, because they are much faster
and there is no possible names conflict. Most of the problems are in choosing right connection
string. Here is an example of tested connection string to MS Access database:
We suppose that your database resides in “Database” subfolder and it name is
“TestDB.mdb”.
You’ll have to use Server.MapPath(“Database\TestDB.mdb”) in order to get physical
location of database.

So connection string would be:

"PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" &


Server.MapPath("Database\TestDB.mdb")
And the way to utilize it:

OleDbConnection conn = null;


OleDbDataReader reader = null;
try
{
conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath("Database/TestDB.mdb"));
conn.Open();

OleDbCommand cmd =
new OleDbCommand("Select * FROM Table1", conn);
reader = cmd.ExecuteReader();

datagrid.DataSource = reader;
datagrid.DataBind();
}
// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
shareimprove this answer
edited May 14 '13 at 13:45
answered May 14 '13 at 13:18

Jake1164
10.2k66 gold badges3838 silver badges5858 bronze badges

 I saw this. but everything VS sayes that & can't be used in string and all is read
– user2362291 May 14 '13 at 13:20
 What does that mean? What cant be used in a string? All what is read? – Jake1164 May
14 '13 at 13:23
 I tried to change con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=" + Server.MapPath("~\\App_Data\\WebSiteDatabase.accdb"); to
"PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" &
Server.MapPath("App_Data\WebSiteDatabase.accdb") and it shows errors
– user2362291 May 14 '13 at 13:25

Try following a tutorial. msdn.microsoft.com/en-us/library/ms971485.aspx and remove the


password until you have it working and then add the password once you are sure you can
connect. – Jake1164 May 14 '13 at
Login Page In ASP.NET Using Access
Database


 Md Sarfaraj
 Feb 13 2019

 12.7k
 0
 3

This blog demonstrates how to create a Login page in ASP.NET with MS Access database using
C# language.

 facebook
 twitter
 linkedIn
 Reddit

o Email
o Bookmark
o
o
o
 expand

Log In page in aspo.net with access


Download Free .NET & JAVA Files API

In this blog, let us see how to create a login page using C# language in an ASP.NET application
that is using MS Access database. Please follow the below steps.

STEP 1

 First of all, open Visual Studio and create a new website.


 Go to the "Website" tab, click on "Add New Item", choose language as "Visual C#" and
select the "Web Form" option.
 Name this page as login.aspx and press the "Add" button.
 Add one more "Web Form" and name that Default.aspx.

STEP 2

Now, design your web pages by trying the below code.

login.aspx

1. <body>
2. <form id="form1" runat="server">
3. <div>
4.
5. <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
6. <asp:TextBox ID="TextBox1" runat="server" Width="146px"></asp:TextBox>
7. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
8. ControlToValidate="TextBox1" ErrorMessage="Enter username." ForeColor="R
ed"></asp:RequiredFieldValidator>
9. <br />
10. Password<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"
11. Width="147px"></asp:TextBox>
12. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
13. ControlToValidate="TextBox2" ErrorMessage="Enter password." ForeColor="R
ed"></asp:RequiredFieldValidator>
14. <br />
15. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Log In"
/>
16. <br />
17. <asp:Label ID="Label2" runat="server"></asp:Label>
18. </div>
19. </form>
20. </body>

After inserting the code, you will get a UI like in the below image.

Default.aspx

1. <body>
2. <form id="form1" runat="server">
3. <div>
4.
5. <asp:Label ID="Label1" runat="server" Text="Welcome"></asp:Label>
6.
7. </div>
8. </form>
9. </body>

STEP 3

Now, let's create our Access database. Here are the details -

 Database Name: LogInDatabase.mdb


 Table Name: login

Insert some data into the login table.

Note - Do not forget to create the database (LogInDatabase.mdb) under your project folder.

STEP 4

After creating the database, let us come back to our Visual Studio project. Double-click the "Log
In" button and add the below code.

login.aspx.cs

First of all, add two classes.

1. using System.Data;
2. using System.Data.OleDb;

Now, add your code.

1. protected void Page_Load(object sender, EventArgs e)


2. {
3. if (!IsPostBack)
4. TextBox1.Focus(); // blink cursor in TextBox1
5. }
6. protected void Button1_Click(object sender, EventArgs e)
7. {
8. string sql; int row;
9. OleDbConnection con = new OleDbConnection();
10. // establish connection
11. con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Serv
er.MapPath("LogInDatabase.mdb");
12. con.Open(); // connection open
13. // sql query
14. sql = "select count(*) from login where user='"+TextBox1.Text+"' and pass='"+Text
Box2.Text+"'";
15. OleDbCommand cmd = new OleDbCommand(sql, con);
16. row = (int)cmd.ExecuteScalar(); // cast into integer and ExecuteScalar() get single v
alue from database.
17. con.Close(); // connection close
18. if (row > 0)
19. Response.Redirect("Default.aspx");
20. else
21. Label2.Text = "Username or Password is invalid...";
22. }

STEP 5

Now, your project is complete. Run your project by pressing F5.

In this blog, I covered the creation of the Login page in ASP.NET with Access database. If you
face a problem with this code, please comment below.

Potrebbero piacerti anche