.net Rookie With Connection To A Database Question

liunx

Guest
I Am Very New To Asp.net...i Have Used Mostly Php My Whole Web-dev Career..but I Am Takigng Initiative To Learn .net. My Question Is..how Do I Connect To A Dabase Through .net....i Have A Folder With A Test Database Called "store.mdb" And I Created A Dsn Also With The Hosting Company's Gui. I Know I Need A Connection String And I Tried Using Legacy Asp Conn Strings But None Work..i Guess A Different Driver Is Needed Right? Feed Back Suggestions Or Tips On .net Are Appreciated. Thanks!hi, u can place ur connection string at either a) Web.config that share accross ur entire application, or b) just make a connection string at ur local class.

i.e.:

private string connectionString = " (ur connect to access, sql, or oracle server goes here)";
private sql = "(ur sql stmnt goes here)";

(if use sql server)
SqlConnection conn = new SqlConnection(connectionString);
(if use oracle...)
OracleConnection conn = new OracleConnection(connectionString);
(if other OLEDB)
OleDbConnection conn = new OleDbConnection(connectionString);

(i use sql for example)
SqlDataReader reader = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
reader = cmd.ExecuteReader();
While (reader.Read()){
(do whatever u need with ur dataset here....)
}//end while
}//end try
catch(System.Exception)
{
return false;
}//end catch
finally
{
if (reader !=null) reader.Close();
if (conn != null) conn.Close();
}//end finallywhere does the connection string stand? and where do you connect to the dsn or you specifiy where the database resides?here's an example of connection string for a sql server, connect to database:Pub, using administrator right:

private string connectionString = "Data Source="localhost;"+
"Initial Catalog=Pubs;User ID=sa;";


if u want...u can put that in ur Web.config, so u don't have to retype this over and over again....
i.e:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSettings>
<add key="ConnectionString" value="Network Library=dbmssocn;Initial Catalog=Pubs;User ID=sa;" />
</appSettings>

<system.web>
.
.
.
</system.web>
</configuration>


and then...in ur aspx page (or ascx) you just need to type:

private string connectionString;
connectionString = ConfigurationSettings.AppSettings["ConnectionString"];


which ever works best for you :)
 
Back
Top