Jump to content

coneverting oledb code to connect to remote sqlserver


sansat6699

Recommended Posts

i have some oledb code made in c#(vs 2005) it is for local msaccess file. i want to conevert the code for sql server where connection string placed in web.config file seperately. please help me.here is code

private void buildGrid() { string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="; conStr += Server.MapPath("~/common/db/demo.mdb"); System.Data.OleDb.OleDbConnection dbCon = new System.Data.OleDb.OleDbConnection(conStr); dbCon.Open(); string sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC"; System.Data.OleDb.OleDbDataAdapter dbAdapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbCon); DataSet ds = new DataSet(); dbAdapter.Fill(ds); ds.Tables[0].TableName = "Inbox"; Grid1.DataSource = ds; }
healp me please
Link to comment
Share on other sites

place your connection string in appSettings

<appSettings>		<add key="ConnectionString" value="Data Source=(local);Initial Catalog=dbname;Trusted_Connection=yes;"/>	</appSettings>

then

private void buildGrid(){	SqlConnection dbCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);	dbCon.Open();		string sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC";	SqlDataAdapter dbAdapter = new SqlDataAdapter(sql, dbCon);	DataSet ds = new DataSet();	dbAdapter.Fill(ds);	ds.Tables[0].TableName = "Inbox";		Grid1.DataSource = ds;}

Link to comment
Share on other sites

place your connection string in appSettings
<appSettings>		<add key="ConnectionString" value="Data Source=(local);Initial Catalog=dbname;Trusted_Connection=yes;"/>	</appSettings>

then

private void buildGrid(){	SqlConnection dbCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);	dbCon.Open();		string sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC";	SqlDataAdapter dbAdapter = new SqlDataAdapter(sql, dbCon);	DataSet ds = new DataSet();	dbAdapter.Fill(ds);	ds.Tables[0].TableName = "Inbox";		Grid1.DataSource = ds;}

thank you man thanks very much i will check it
Link to comment
Share on other sites

I put it in appSettings and use the code I showed you earlier.

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">	<appSettings>		<add key="ConnectionString" value="Data Source=(local);Initial Catalog=dbname;Trusted_Connection=yes;"/>	</appSettings>......</configuration

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...