Jump to content

coneverting oledb code to connect to remote sqlserver


sansat6699
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...