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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vijay YadavPosted Apr 27, 2011, 1:17 AM
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.
Mayur GujrathiPosted Apr 27, 2011, 1:06 AM
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());
}
Shalini JunejaPosted Apr 27, 2011, 12:44 AM
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