Database connect and retrieve in c# [Solved/Closed]

- May 20, 2009 at 02:39 AM - Latest reply:  decent10_cs
- Jun 14, 2010 at 01:23 AM
Hello,
I am ilayaraja doing my project work,i have one problem
my project doing in asp.net
but I don't know how to connect the database in asp.net using c# and how to retrieve in database to web application .
please give the answer my question, urgently.
See more 

12 replies

Best answer
Posts
308
Registration date
Tuesday November 18, 2008
Last seen
February 2, 2010
- May 20, 2009 at 03:54 AM
109
Thank you
the code is

public void ConnectToAccess()
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
database. conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data source= @"path \AccessFile.mdb";
try
{
conn.Open();
// Insert code to process data.
}
catch (Exception caught)
{
MessageBox.Show(caught.Message);
}
finally
{
conn.Close();
}
}

Thank you, epsiman 109

Something to say? Add comment

CCM has helped 1667 users this month

missing a double quote somewhere
Are you sure is this correct? I'll try to do this my self and send a reply to if positive.
database. conn.ConnectionString = @'Provider=Microsoft.Jet.OLEDB.4.0; Data source= @'path \AccessFile.mdb";
46
Thank you
Notice this line
-> database. conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data source= @"path \AccessFile.mdb";

Should be
database. conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=AccessFile.mdb";



Other Connection String Sample.

Visual Basic]
Public Sub CreateSqlConnection()
Dim myConnection As New SqlConnection()
myConnection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30"
myConnection.Open()
End Sub 'CreateSqlConnection

[C#]
public void CreateSqlConnection()
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30";
myConnection.Open();
}

[C++]
public:
void CreateSqlConnection()
{
SqlConnection* myConnection = new SqlConnection();
myConnection->ConnectionString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30";
myConnection->Open();
}
using System.Data.Oledb;

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
}
}
}
}
18
Thank you
string str = "server=.;database=master;uid=sa;password=;";
SqlConnection con = new SqlConnection(str);
con.Open();

This is only for connecting database
12
Thank you
How To Create Connection BetWeen C#.net 2005 (Window Application) And MS Access 2003
With Some Example

Hope!

Thank-You!!!!!!
10
Thank you
using System.Data.Oledb;

namespace cons
{
pubic class OledbConnection
{
public void do()
{
OledbConnection con= new OledbCOnnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database.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())
{
/// Do some coding here
}
- Jun 14, 2010 at 01:23 AM
8
Thank you
1. First of all you have to import required namespace , in case is for sqlserver is sqlclient by using directive.

using System.Data.SqlClient;

2. Create object of sqlconnection class

private SqlConnection _Connection = New SqlConnection();

3. then Assign the path to sqlserver server by setiing ConnectionString property to the sqlserver path.

_connention.ConnectionString = " Data Source=.\\Servername;
Initial Catalog=DatabaseName; Integrated Security=True";

4. then Open connection by calling Open() method

_connection.Open();

Here we go
3
Thank you
i have tried
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=Db.mdb");
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand("SELECT * from temp", conn);


but it gives an error on conn, as if the object has not been created...plzzz can anyone help me..thanx in advance
0
Thank you
code to connection of database