Okay I'm working on a C# web app and I am trying to test the connection to my oracle database. I would like to send the connection state to a label. I don't know if I am using the commands correctly (this is my first time using an oracle connection with C#). Here is my code:
OracleConnection conn = new OracleConnection();
conn.ConnectionString = "Data Source=servername; Initial Catalog=dbname; User ID=userid; Password=password; Integrated Security=no;";
conn.Open();
string connstate;
conn.State.ToString(connstate);
Label_connectiontest.Text = connstate;
It is just
Label_connectiontest.Text = conn.State.ToString();
The State property of a IDbConnection object is an enum of type ConnectionState with the FlagAttribute set.
Applying the ToString method to this enum results in returning a value that is a string containing a delimiter-separated list of the names of the constants.
Related
I am trying to connect to SQL Server and get data. This is what I did, but it's not working:
string connectionString;
SqlConnection cnn;
connectionString = #"Data Source=(IP)\PC-NAME\SQLEXPRESS,3306;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password";
cnn = new SqlConnection(connectionString);
cnn.Open();
MessageBox.Show("Connection Open !");
cnn.Close();
Your Code is Correct, except your connection string i think
So, first, connect to your database via server Explorer in VisualStudio\View menu
Then right-click on your database and select properties and check the connection string and copy that for test
I think you have a problem with your connection string.
Check your connection string using this given example:
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial
Catalog=myDataBase;User ID=myUsername;Password=myPassword;
I have to finalize a software with local database automatically installed when any one setup the final.exe program, but I cannot connect and design code with my local database.
class Class1
{
internal static string x = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Prog\Try\StrorPro_v1.3\StrorPro_v1.3\StoreProData_v1.2.mdf;Integrated Security=True;Connect Timeout=30";
}
using (SqlConnection cn = new SqlConnection(Class1.x)) {
cn.Open();
string cm = "select id from item_new_customer where cust='" + textBox2.Text +
"' order by id";
using (SqlCommand cmd = new SqlCommand(cm, cn)) {
...
Your connection string should look similar to the example below. It isn’t necessary to reference an MDF or other file. Data Source refers to the SQL Server instance where the database is hosted. Initial Catalog will be the database that all SQL statements without the USE keyword or a three part identifier, i.e. Database.Schema.Table, are sent to. When SSPI is used for Integrated Security the Windows credentials that are used to run the application will be used in the authentication process. True is equivalent to SSPI, however it’s recommended to use SSPI. To use SQL Server authentication specify a user ID and password for the User ID and Password properties, and set Integrated Security to false. For more details on additional connection string properties you can refer to the documentation here.
“Data Source=YourSQLServerInstance;Initial Catalog=YourDatabase;Integrated Security=SSPI;"
I tried to create SqlConnection object with connection string such as
SqlConnection con = new SqlConnection("Connection string");
However, when the program runs, exception happens, and the exception says
the keyword, port, is not supported.
So I change connection string from
server=server name;user id= user name;password=myPassword;persistsecurityinfo=False;database=Database name;Port=port number
to
server=server_name, portNum;user id= user_name;password=myPassword;persistsecurityinfo=False;database=Database_name;
or to
server=server_name: portNum;user id= user_name;password=myPassword;persistsecurityinfo=False;database=Database_name;
But I still have problems to connect to database.
Could anyone give me how to connect database with port number?
Sincerely,
You can use the port at the end of the data source
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
Check http://www.connectionstrings.com/sql-server/
I am trying to create an application which will synchronize its database with another software's database..
Problems are:
How will the database will synchronize with external database after I have created the installer and installed..i.e. how will it take the connection string.
I am looking for a solution which will provide a button to select the required database and based on the selection automatically generate the connection string.
Is it possible to run SQL create queries on external database while installation after the database is selected via browse button?
You have an option to create a Connection Settings form, which will help user to construct the connection string.
Yes, it's possible. It looks something like this:
using (var sqlConnection = new SqlConnection(#"Data Source=myServerAddress;User Id=myUsername;Password=myPassword;"))
{
sqlConnection.Open();
string query = "CREATE DATABASE...";
using (var sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = query;
sqlCommand.ExecuteNonQuery();
}
}
If it won't work, try to set Initial Catalog to master:
#"Data Source=myServerAddress;Initial Catalog=master;User Id=myUsername;Password=myPassword;"
I'm encountering a problem with my database connection.
I started with a new blank solution, then I added a WCF library project, and last but not least a WCF website (service).
In the website i added a reference to the library where I have the interface (data contract), the class that implements the interface and the dataclasses.
what I'm trying to do is to connect to a database on a server and try to retrieve some data from there.
So the connection string looks like:
<add name="myConnectionString" connectionString="Data Source=MyServer; Initial Catalog=MyDatabase; User Id=me; Password=me123;" providerName="System.Data.SqlClient" />
and this is how I'm trying to connect with the database:
public List<string> GetEngagements(string id)
{
string sql = "SELECT myColumn FROM myTable WHERE Id = '" + id + "'";
string connString = string.Empty;
SqlConnection connDB;
connString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
connDB = new SqlConnection(connString);
SqlCommand command = new SqlCommand(sql, connDB);
connDB.Open();
SqlDataReader rdr = command.ExecuteReader();
List<string> numbers = new List<string>();
while (rdr.Read())
{
numbers.Add(rdr[0].ToString());
}
rdr.Close();
return numbers;
}
I'm getting an exception on connDB.Open().
Then exception message says:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
I've been getting this message for 2 days now, I've googled a lot and deleted the C:\Documents and Settings\username\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS directory but it didn't work for me..
Any solution???? help please
The error message:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance.
Suggests that you're using user instancing, and therefore your connection string will point to an .mdf file on disk rather than the name of a database.
So I'll assume that you want to connect to a file instance rather than a server instance.
I'll also assume that you're using SqlExpress rather than the full fat version.
In which case your connection string is wrong. It should look more like this:
"Data Source=.\SQLEXPRESS;
AttachDbFilename=fileOnDisk.mdf;
Integrated Security=True;
User Instance=True;"
User instancing means that this server instance and the DB inside will only be visible to the application opening the connection string.
You don't have to use user instancing - you can set User Instance=False or just leave it out. Then once the application has made the connection you can connect other tools to the server instance and connect to the DB yourself.