Automatically Detect sql Database
what is the code to automatically detect sql database in Any System.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Shalini JunejaRank #351 · 7.1K reputationPosted Apr 26, 2011, 8:44 PM EDT
C#
// Connection strings shouldn't be hardcoded for production code
using(SqlConnection conn = new SqlConnection(
"server=MyServer; database=AdventureWorks; user id=MyUser; password=MyPassword")) {
conn.Open();
SqlCommand cmd = new SqlCommand(
"SELECT Name, GroupName FROM HumanResources.Department", conn);
SqlDataReader r = cmd.ExecuteReader();
while(r.Read()) {
// Consume the data from the reader and perform some computation with it
}
}
Visual Basic .NET
Dim cmd as SqlCommand
Dim r as SqlDataReader
' Connection strings shouldn't be hardcoded for production code
Using conn As New SqlConnection( _
"server=MyServer; database=AdventureWorks; user id=MyUser; password=MyPassword")
conn.Open()
cmd = New SqlCommand("SELECT Name, GroupName FROM HumanResources.Department", conn)
r = cmd.ExecuteReader()
Do While r.Read()
' Consume the data from the reader and perform some computation with it
Loop
End Using
This sample uses the System.Data.SqlClient provider to connect to SQL Server. Note that if this code runs inside SQLCLR, it would be connecting from the SQL Server that hosts it to another SQL Server. You can also connect to different data sources. For example, you can use the System.Data.OracleClient provider to connect to an Oracle server directly from inside SQL Server.
Hope this will help you
Mayur GujrathiRank #666 · 3.6K reputationPosted Apr 26, 2011, 9:06 PM EDT
This will test the connection of oracle database
private void button1_Click(object sender, EventArgs e)
{
oracon = new OleDbConnection("Provider=Msdaora;User Id=Scott;Password=tiger");
oracon.Open();
MessageBox.Show(oracon.State.ToString());
oracon.Close();
MessageBox.Show(oracon.State.ToString());
}
this will test the connection of sql server database
private void button2_Click(object sender, EventArgs e)
{
sqlcon = new OleDbConnection();
sqlcon.ConnectionString = "Provider=SQLOLEDB;User Id=citrus_usr;Password=surtic;Database=DEMAT;Data Source=server";
sqlcon.Open();
MessageBox.Show(sqlcon.State.ToString());
sqlcon.Close();
MessageBox.Show(sqlcon.State.ToString());
}
Vijay YadavRank #1,637 · 943 reputationPosted Apr 26, 2011, 9:17 PM EDT
Try this,
string strCon = "Data Source=.\\SQLExpress;AttachDBFilename=|DataDirectory|db_name.mdf;integrated security=true;" and place the database file inside bin folder if you are using window application, if you are using web application then place in App_Data folder.