.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

connect sql database

Posted By: kokulakrishnan     Posted Date: December 22, 2010    Points:2   Category :ASP.Net
can any one explain the codes regarding to c# asp.net to connect sql database.actually this is my beginning stage for asp.net.


thank you

Responses
Author: Syed Shakeer Hussain             
Posted Date: December 22, 2010     Points: 5   
Author: Naresh             
Posted Date: December 24, 2010     Points: 5   

hi,

Here is a coding just to insert to a table in sql database,,


Add this ,
using System.Data.SqlClient;

In the button click event type this coding,

string sql = "Server=127.000.000.001;DataBase=DataBaseName;Trusted_Connection=Yes;";
SqlConnection con = new SqlConnection(sql);
con.Open();
string str = "";
str = "INSERT INTO [TABLENAME]([Title],[LastName],[FirstName],) VALUES ('" +
txtTitle.Text.Trim() + "','" + txtLastName.Text.Trim() + "','" +
txtFirstName.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.close();

Thanks
Naresh
Author: Karthikeyan Anbarasan       [Moderator]      
Posted Date: December 25, 2010     Points: 5   

Hi,

The lifetime of a SqlConnection are as below:

Instantiate the SqlConnection object.
Open the connection.
Perform DB operations.
Close the connection.

Code sample:
using System;
using System.Data;
using System.Data.SqlClient;

class SqlConnectionTest
{
static void Main()
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
SqlDataReader rdr = null;

try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Customers", conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}

Thanks
Karthik
http://www.f5debug.net
Mark as Answer if the answer solves your issue!!!
Author: SNK111             
Posted Date: December 28, 2010     Points: 5   

To connect to SQL Server from C#.NET, you need to create a connection string such as below:

private SqlConnection connection;
private string connectionString =
@"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );

Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below:
SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid", connection);

The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.

Next to execute the SQL queries in the database, you use the following methods:
ExecuteReader - to execute SELECT queries
ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database.
For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html )
Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.

Author: kokulakrishnan             
Posted Date: December 28, 2010     Points: 5   

thanks for gave guidance to my question.

thank you very much



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend