In WEB CONFIG....
SYNTAX
<add name="ConnectionStringName" connectionString=YourServer"; Initial Catalog=YourDB; Integrated Security=True"/>
EXAMPLE :
<connectionStrings>
<add name="Con" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
We can use this connection string in our project.....
1.First Method
SqlConnection con = new SqlConnection("data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" );
SqlCommand cmd = new SqlCommand("Write your sql query here eg. select * from Table name");
con.Open();
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();
2.2nd method
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=DatabaseName;User ID=username;Password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>
Add a Class File
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public class WFDFeedsNews
{
SqlConnection con=new SqlConnection();
SqlCommand cmd=new SqlCommand();
public createConnection()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings ["connectionStringName"].ConnectionString);
}
public insertData(int id,string name)
{
con.Open();
try
{
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO (userID, HeadLine)
VALUES (@id,@headline)";
cmd.Parameters.AddWithValue("@id",id);
cmd.Parameters.AddWithValue("@headline",name);
int result=con.executeNonQuery();
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}
}
}
Pass data from aspx page to this code...
SYNTAX
<add name="ConnectionStringName" connectionString=YourServer"; Initial Catalog=YourDB; Integrated Security=True"/>
EXAMPLE :
<connectionStrings>
<add name="Con" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
We can use this connection string in our project.....
1.First Method
SqlConnection con = new SqlConnection("data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" );
SqlCommand cmd = new SqlCommand("Write your sql query here eg. select * from Table name");
con.Open();
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();
2.2nd method
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=DatabaseName;User ID=username;Password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>
Add a Class File
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public class WFDFeedsNews
{
SqlConnection con=new SqlConnection();
SqlCommand cmd=new SqlCommand();
public createConnection()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings ["connectionStringName"].ConnectionString);
}
public insertData(int id,string name)
{
con.Open();
try
{
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO (userID, HeadLine)
VALUES (@id,@headline)";
cmd.Parameters.AddWithValue("@id",id);
cmd.Parameters.AddWithValue("@headline",name);
int result=con.executeNonQuery();
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}
}
}
Pass data from aspx page to this code...
No comments:
Post a Comment