I'm trying to connect to a DB and I followed the connection string suggestions. However, I'm not a DB-guy so some things are, hrmp... less than obvious.
For instance, the DB server is within the network and the connection to it goes from another server, also in the same network. I've used the server name and port to connect to it using Management Studio so it's up and running.
This is my connection string.
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString =
"integrated security=SSPI;"+
"server=server.name.as.in.management.studio,4340" +
"persist security info=False;database=NameOfTheDb";
The authentication is done using AD and the error message is 40 - can't find the server. Besides the obvious - the server can't be found - what can I do to trouble-shoot this, obtain more information etc.?
I'm at a customer and their system is not as well documented as one'd like. I get very little information and the coverage is questionable. The person who set up the attrocity is gone since a long time.
Suggestions are welcome.
EDIT
Following the corrections provided, I'm getting error code 0 - The requested name is valid but no data of the requested type was found.
What do I do with this?! :)
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString =
"integrated security=SSPI;"+
"Data Source=myServerAddress;" +
"persist security info=False;" +
"Initial Catalog=NameOfTheDb";
Hope this helps.
Sure servce in Managmnet Sql Server is Start.
You can do that by go to ---> Control Panel-->All Control Panel Items-->Administrative Tools--Services --> SQL Server(MSSQLSERVER) and click Start.
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlDbConnection();
conn.ConnectionString =
"Data Source=ServerName;" +
"Initial Catalog=DataBaseName;" +
"User id=UserName;" +
"Password=Secret;";
conn.Open();
Related
I'm currently trying to build an application in C# and connecting it to a live db running in Oracle 11g.
I have the following connection details
Host IP: 10.204.1.3
Port: 1521
DB Name: PROD
My source code
string connString = "DATA SOURCE=10.204.1.3:1521/PROD;PERSIST SECURITY" +
"INFO=True;USER ID=username; PASSWORD=userpass";
OracleConnection conn = new OracleConnection(connString);
conn.Open();
I was able to add a connection in Server Explorer with the Connection String used by VS but is having the error below in conn.Open();
An unhandled exception of type 'System.NullReferenceException' occurred in
Oracle.DataAccess.dll
Sorry if this is a basic question, I'm new in VS, and Oracle and can't find the solution in the other part of the web. Thanks in advance.
My code is now working. I should've read the Oracle documentation (reference below).
string connString = "DATA SOURCE=10.204.3.1:1521/PROD;" +
"PERSIST SECURITY INFO=True;USER ID=username; password=password; Pooling
=False;";
OracleConnection conn = new OracleConnection();
conn.ConnectionString = connString;
conn.Open();
Reference: http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/appdev/dotnet/Web_version_Fully_Managed_ODPnet_OBE/odpnetmngdrv.html
I am create a database utility and I seem to not be able to get my connectionstring correct.
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=.\\SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=|DataDirectory|ConfigurationData.mdf;";
I believe this is in the correct format. As for the data source, my sql server is SQLExpress which runs sql server 2008 R2. My database is named ConfigurationData. Am I missing something?
When I run it, it opens the database - I assume it does since it does not through exception - but when I try inserting into a table, it does not actually insert it yet it executes the command.
conn.Open();
try
{
SqlCommand comm = new SqlCommand("INSERT INTO Test " + "(id,number) " + " VALUES(" + 10 + " , " + 12 + ")", conn);
comm.ExecuteNonQuery();
Console.WriteLine("Database is created successfully", "MyProgram");
}
catch (Exception ex)
{
}
finally
{
if ((conn.State == ConnectionState.Open))
{
conn.Close();
}
}
EDIT
Just remembered that I had answered a similar question a while back. Check it out:
Why can't I insert data into local database (SQL Compact Edition) with C#?
I don't think it is the connection string issue. But for your reference, a good site to refer to is http://www.connectionstrings.com/sql-server-2008/
You would need one of these:
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;
Attach a database file on connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;
Using an User Instance on a local SQL Server Express instance
The User Instance functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL
Server instance to a user with limited administrative rights on the computer.
Data Source=.\SQLExpress;Integrated Security=true;
AttachDbFilename=C:\MyFolder\MyDataFile.mdf;User Instance=true;
To use the User Instance functionality you need to enable it on the SQL Server. This is done by executing the following command: sp_configure 'user instances enabled', '1'. To disable the functionality execute sp_configure 'user instances enabled', '0'.
try (local) instead of dot, dot is not recognized in Win XP
conn.ConnectionString =
"Data Source=(local)\\SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=|DataDirectory|ConfigurationData.mdf;";
You're SQL Statement isn't right also, and you should use parameters, but here is what you should have
SqlCommand comm = new SqlCommand("INSERT INTO Test (id, number) VALUES('" + 10 + " ', '" + 12 + "')", conn);
Why not just use the SqlConnectionStringBuilder class?:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = #"(local)\SQLExpress";
builder.UserInstance = true;
builder.IntegratedSecurity = true;
builder.AttachDBFilename = "|DataDirectory|ConfigurationData.mdf";
SqlConnection conn = new SqlConnection(builder.ConnectionString());
The output:
"Data Source=(local)\\SQLExpress;AttachDbFilename=|DataDirectory|ConfigurationData.mdf;Integrated Security=True;User Instance=True"
One of the way to do this is to add your connection string in web.config file as shown below:
Jus click on the properties of database on the database explorer. There you will find connectionstring in its properties. Jus add it in connectionstring below.
<configuration>
<connectionStrings>
<add name="ConnectionName" connectionString="your connection string" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Then in the page you can store it in string or directly refer to your connection as shown below:
Connection con=new SqlConnection();
con.ConnectionString=ConfigurationManager.ConnectionStrings["connString"].ToString();
and I suspect your insert statement is not properly declared.
Just try this:
SqlCommand comm = new SqlCommand("INSERT INTO Test (id,number) VALUES('10' ,'12')", con);
That's all from my part... Hope it helped you..
I use the code bellow to connect to my database on my website:
string tempstr = "Data Source=" + "72.55.---.---" + ";Initial Catalog=-------;Integrated Security=False;Persist Security Info=True;User ID=" + "MYUSER" + ";Password=" + "MYPASS";
SqlConnection con = new SqlConnection(tempstr);
con.Open();
Is it secure enough ? or someone who is not professional hacker can capture the username and the password which is sent from his/her computer to my database !?
thanks in advance.
Strings like the above will be stored in plain text in the executable.
If the "attacker" has access to the DLL, it is a simple thing to dump all the strings contained in it - including the connection string.
If you want to secure the connection string, a common practice is to store it in the connectionStrings section of the application .config file and encrypt it.
Suppose I have created a SQL Server database called Database1.mdf in the App_Data folder in Visual Studio with a table called Names.
How could I establish a connection to read the table values using C#?
So far I've tried something like this:
SqlConnection conn = new SqlConnection("Server=localhost;Database=Database1;");
conn.Open();
// create a SqlCommand object for this connection
SqlCommand command = conn.CreateCommand();
command.CommandText = "Select * from Names";
But I get an error:
database not found/error connecting to database
In Data Source (on the left of Visual Studio) right click on the database, then Configure Data Source With Wizard. A new window will appear, expand the Connection string, you can find the connection string in there
If you use SQL authentication, use this:
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=.\SQLExpress;" +
"User Instance=true;" +
"User Id=UserName;" +
"Password=Secret;" +
"AttachDbFilename=|DataDirectory|Database1.mdf;"
conn.Open();
If you use Windows authentication, use this:
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=.\SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=|DataDirectory|Database1.mdf;"
conn.Open();
If you're using SQL Server express, change
SqlConnection conn = new SqlConnection("Server=localhost;"
+ "Database=Database1;");
to
SqlConnection conn = new SqlConnection("Server=localhost\SQLExpress;"
+ "Database=Database1;");
That, and hundreds more connection strings can be found at http://www.connectionstrings.com/
SqlConnection c = new SqlConnection(#"Data Source=localhost;
Initial Catalog=Northwind; Integrated Security=True");
You try with this string connection
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|Database1.mdf;Database=dbname; Trusted_Connection=Yes;
I like to use the handy process outlined here to build connection strings using a .udl file. This allows you to test them from within the udl file to ensure that you can connect before you run any code.
Hope that helps.
Visual Studio 2019 (and probably a few previous versions).
View -> SQL Server Object Explorer
Top of the tree is 'SQL Server'
Under 'SQL Server', are couple of '(localdb)....'
Expand the (localdb)... -> Databases until you find your db.
Database Name (eg. Database1) -> Right-click -> Properties, and scroll the many properties (eg. "ANSI
NULL Default"). Find the "Connection string" property, copy the value
into your code, and you're running.
My code is as follows:
string constring = "Data Source=132.186.127.169"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";
SqlConnection con;
con = new SqlConnection(constring);
con.Open();
string query="SELECT * from CadPoolProjectTable1";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
MessageBox.Show("selected");
con.Close();
I am getting error at the line con.Open();. The error is:
A network-related or instance-specific
error occurred while establishing a
connection to SQL Server. The server
was not found or was not accessible.
Verify that the instance name is
correct and that SQL Server is
configured to allow remote
connections. (provider: Named Pipes
Provider, error: 40 - Could not open a
connection to SQL Server)
You are missing a ';' after the server name in the connection string.
string constring = "Data Source=132.186.127.169"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";
It should be
string constring = "Data Source=132.186.127.169;"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";
The error says that your app was not able to connect to the server. I would do the following.
Check for the server address (on a quick look it looks good)
Connect using management studio and in your case it should have worked.
It means the issue is with the code. Since you are concatenating the string I would debug the code and see what the end result for the connection string is.
Tip:If it is a web application add the connection string to web.config file. More info here How to: Read Connection Strings from the Web.config File
You're missing a semicolon in your connection string
Data Source=132.186.127.169;"+ "Initial...
^
If you need to build the connection string yourself you can use the SqlConnectionStringBuilder class. That way you won't to be as troubled by these subtle mistakes.
Your connection string is wrong:
string constring =
"Data Source=132.186.127.169;Initial Catalog=CadPool1;Integrated Security=True";
You don't need to concatenate the strings together, but more importantly, you were missing the semi-colon ";" between the data source and the initial catalog settings.
First, you are missing a ; (semicolon) between Data Source and Initial Catalog.
Second, if this is a newly installed SQL Server instance, you may need to go into SQL Server Configuration Manager, and enable the protocol(s) you'll need.
Please check if you can ping the server mentioned via cmd
Also try telnet to the server from your machine.
One more thing to check would be port the server is configured for if its not the default one you will have to add the port as 132.186.127.169,XXX
Servername in the connection string is wrong. and also since there are no dynamic values you don't need string concatenation. Change it to:
string constring = "Data Source=132.186.127.169;
Initial Catalog=CadPool1;
Integrated Security=True";