Connecting to Database?  
Author Message
Musafir





PostPosted: Visual C# General, Connecting to Database? Top

Which is best way to write the connection statements while connecting to the database(sql server for eg.) Consider a button to add data, another button to delete data.. please provide your valuable suggestions in implementing the database using c# for web based applications..

Visual C#19  
 
 
ahmedilyas





PostPosted: Visual C# General, Connecting to Database? Top

well really there is only 1 way of doing this. it also depends the type of authentication you are using to connect to the database.

SqlCommand theSQLCommand = new SqlCommand("INSERT STATEMENT HERE", new SqlConnection("ConnectionStringHere"));

theSQLCommand.Connection.Open();

theSQLCommand.ExecuteNonQuery();

theSQLCommand.Connection.Close();

www.connectionstrings.com would be one place you can obtain the connection strings for SQL Server, amoungst other applications.

small example of deleting data....

SqlCommand theSQLCommand = new SqlCommand(" ", new SqlConnection("ConnectionStringHere"));

theSQLCommand.Parameters.Add(theParam);

theSQLCommand.Connection.Open();

theSQLCommand.ExecuteNonQuery();

theSQLCommand.Connection.Close();