Report

How to make database connectivity in asp.net [Solved/Closed]

Ask a question Digvijaysinh 10Posts Tuesday January 27, 2009Registration date July 6, 2009 Last seen - Last answered on Apr 10, 2017 at 05:36 AM by abhishek
Hello,
i m looking for the database connectivity in asp.net with sql server 2005 or ms access 2007?
please anybody solve my query.
thanks in advance, looking for your response.
See more 
Helpful
+371
plus moins
hi,
please add the follwoing code in you aspx.cs page.

Step 1 : Add Namspace "using System.Data.SqlClient;"

Step 2 : Make Sql connection.

Write this code to create Sql connection.
SqlConnection con = new SqlConnection("Server=You server name or comp name;Database=Yourdatabasename;Trusted_Connectopn=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();

i hope this should work
Was this answer helpful?  
iqbal- Aug 18, 2014 at 11:41 AM
it shows error on con.open(); as sqlexception was unhandled
ravi- Feb 2, 2017 at 03:59 AM
that code must be write in page load method
Reply
pratap- Dec 10, 2015 at 06:41 AM
this code is use full thank you
abhishek- Apr 10, 2017 at 05:36 AM
this code was very helpful
Reply
Helpful
+90
plus moins
Hi, following is code for database connectivity in asp.net using C# and and SQL Server.

While creating the database connection, consider the following things in mind.

1) Add the namespace
- System.Data
- System.Data.SqlClient.

2) Create connection object using connection parameters like server name, database name , user name and password.


3) Create sqlcommand object and pass connection object and sql query as parameters

4) Execute the command using ExecuterReader() method of sqlcommand object.
this will return resultset and get it into sqldatareader object.

5) Navigate the resultset.

6) close the connection object as well as command object

Following is the code:

Using System.Data;
Using System.Data.SqlClient;



SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader rdr ;
try
{
con = new SqlConnection("Data Source = Server Name; Initial Catalog = Database Name; User Name = user name; password = password");

con.Open();
cmd = new SqlCommand(con,"sql query");
rdr = cmd.ExecuteReader();
while(rdr.Read())
{
// Operate on fetched data
}
}
catch(Exception exp)
{
MessageBox(exp.Message);
}
finally
{
if(cmd != null)
cmd.Close();
if(con != null)
con.Close();
if(rdr != null)
rdr.Dispose();
}
linlin- May 6, 2010 at 10:48 PM
I write like this .but error
con= new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=POS; Integrated Security=SSPI");
Ravi- Apr 23, 2014 at 04:06 PM
Integrated Security should be true
Helpful
+55
plus moins
System.Data.SqlClient;
SqlConnection conn = new SqlConnection("Data Source=servername;Initial Catalog=DatabaseName;User ID=sa;password=pwd;Integrated Security=True");



Open Connection.
User SqlDataAdapter if
Helpful
+52
plus moins
Im Having The Same Sort Of Problem With MySql Im Using This Code below And It Keeps Showing Errors On Line 30 If I Can Fix It It May Work For You Too Worth A Shot ?: For A P-Server Call Conquer Online If Your Wondering But I Dont Know If You Can Take Bits Out And Your For Yourself

using System;
using MySql.Data.MySqlClient;

namespace CoEmu_v2_LoginServer.Database
{
/// <summary>
/// Provides MySql resource connections, for multiple connections to a single MySql database.
/// This is due to the fact that the server is multi-threaded, so allowing a single connection would
/// likely result in errors, or general loss of performance.
/// </summary>
public static class DatabaseConnection
{
public const string USER_NAME = "localhost";
public const string PASSWORD = "1234";
public const string SERVER = "127.0.0.1";
public const string DATA_BASE = "conqueremu";
public static MySqlConnection NewConnection()
{
MySqlConnection C = null;
try{
C = new MySqlConnection("Server=" + SERVER + ";Database='" + DATA_BASE + "';Username='" + USER_NAME + "';Password='" + PASSWORD + "'");
C.Open();
}
catch(Exception.e)
{
Console.WriteLine(e.ToString());
return null;
}
return C;
}
}
}
Helpful
+43
plus moins
please add the following code in you aspx.cs page.

Step 1 : Add Namspace "using System.Data.SqlClient;"

Step 2 : Make Sql connection.

Write this code to create Sql connection.
SqlConnection con = new SqlConnection("Server=You server name or comp name;Database=Yourdatabasename;Trusted_Connectopn=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();
rahul- Jul 4, 2010 at 01:19 PM
thanks dude
raj- Jul 4, 2010 at 01:19 PM
really nice work thanks
sonu- Jul 4, 2010 at 01:20 PM
tusi great ho sir
Helpful
+32
plus moins
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("/db/northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DataList
id="customers"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="True"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
footerstyle-font-size="9pt"
footerstyle-font-italic="True">

<HeaderTemplate>
Customers Table
</HeaderTemplate>

<ItemTemplate>
<%#Container.DataItem("companyname")%> in
<%#Container.DataItem("address")%>, <%#Container.DataItem("city")%>
</ItemTemplate>

<FooterTemplate>
Source: Northwind Database
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>
Helpful
+31
plus moins
Hi there is al ink by microsoft that I think can help you much on this issue http://www.microsoft.com/sqlserver/2008/en/us/express.aspx
Digvijaysinh- Jan 29, 2009 at 05:47 AM
Thanx very much, I think I have got my task right.
Helpful
+27
plus moins
if you want to make connectivity with sql server on the local system.just define the connection string
sqlconnection con=new sqlconnection("server=. database="specify the datbase in your system",uid=sa;pwd=sa;");

add namespace using system.dat.sqlclient;

happy coding
Helpful
+24
plus moins
Hi,
please add the following code in you aspx.cs page.

Step 1 : Add Namspace "using System.Data.SqlClient;"

Step 2 : Make Sql connection.

Write this code to create Sql connection.
SqlConnection con = new SqlConnection("Server=You server name or comp name;Database=Yourdatabasename;Trusted_Connectopn= 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();

i hope this should work
Helpful
+19
plus moins
I have constructed a form in aspx (form.aspx). I have constructed datafile in access. I have imported file into root of my web file. When I view form in browser and fill in fields and press 'transmit' info stays in the form and does not connect with datafile.

Can somebody please help
Helpful
+12
plus moins
Use a .UDL object, textual edit to see connection string
Helpful
+12
plus moins
go through the book available in the market the best option for this

thanks
Helpful
+10
plus moins
Thanku ya.
Helpful
+8
plus moins
Please visit the link below for details

link
Gman- Apr 1, 2010 at 10:49 AM
Dim myConn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
kketann 1Posts Tuesday April 27, 2010Registration date April 27, 2010 Last seen - Apr 27, 2010 at 04:40 AM
download our free dating software worth over $350 for free
asp.net with sql server
software is available with full source code

www.websol-dating-software.com
pawan- Oct 29, 2012 at 07:56 AM
what should be the data source name if I dont have any sql server . I want to use built in sever of asp.net . please tell me.
Helpful
+3
plus moins
try this......

SqlConnection con = new SqlConnection(strcon);
con.Open();
ds = db.get_Data("SELECT ID, user_name, password FROM login WHERE (CAST(user_name AS varbinary(50)) = CAST('" & loginusertxt.Text.ToString().Trim() & "' AS varbinary(50))) AND (CAST(password AS varbinary(50)) = CAST('" & loginpasstxt.Text.ToString().Trim() & "' AS varbinary(50))) AND Deleted = 'False'")
SqlCommand cmd = new SqlCommand("select id,u_nm,pass from login WHERE (CAST(u_nm As varbinary(50))= CAST ('" + txtunm.Text + "' As varbinary(50)) AND (CAST(pass AS varbinary(50)) = CAST('" + txtpass.Text + "' As varbinary(50)) ) " ,con);
SqlDataAdapter adpt = new SqlDataAdapter("select id,u_nm,pass from login WHERE (CAST(u_nm As varbinary(50))= CAST ('" + txtunm.Text + "' As varbinary(50)) AND (CAST(pass AS varbinary(50)) = CAST('" + txtpass.Text + "' As varbinary(50)) ) ",con);
DataSet ds = new DataSet();
adpt.fill(ds,"login");
adpt.Fill[ds, "login"];
adpt.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0].ItemArray[1].ToString().Trim() = txtunm.Text.ToString().Trim() && ds.Tables[0].Rows[0].ItemArray[2].ToString().Trim() = txtpass.Text.ToString().Trim())
{
Response.Redirect("index.aspx");
}
}
Helpful
+1
plus moins
You Can Make Database Connectivity Through two ways :

1- after implementing namespace using System.Data.SqlClient;
Create SsqlConnection sqlconnection cnn=new sqlconnection(" type here the connection string of the database");

and after this you can open connection and do operations you want on this connection .

2- You can make your connection is Dynamic through the following processes :

2-1 : open web.config file
2-2 : after <AppSettings/> tag , delete this tag and write new tag with the same name to show this tage <AppSettings>.....</AppSesstings>

2-3 : write inside previous tag this line

<appSettings>
<add key="connectionString" value="Data Source Of Your Database"/>
</appSettings>

2-4 : Good , now your connection it is Dynamic And More Secure .

2-5 : To Access To This connection Open The Page.cs Such As Default.cs and write :

using System.Data.SqlClient;

public SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connectionString"].ToString());

and do your operation you want without showing your database connection to secure issues .


Good Luck .

Regards : Hussein Nasser Al-Qadri

Member requests are more likely to be responded to.

Members can monitor the statuses of their requests from their account pages.

A CCM membership gives you access to additional options.

Not a member yet?

Sign up now. It takes less than a minute and is completely free!