connecting with Sqlexpress - c#

i am trying to connect the sql with a database on server. So when i entered the data source. its showing me Error Unrecognized escape sequence
Please help me out.
string constring = "Data Source=DVSQL\SQLEXPRESS;Initial Catalog=DB;User ID=user;Password=****";
i tried below both ways then its showing me Error Unable to connect any of specified SQL host
string constring = "Data Source=DVSQL\\SQLEXPRESS;Initial Catalog=DB;User ID=user;Password=****";
string constring = "Data Source=DVSQL/SQLEXPRESS;Initial Catalog=DB;User ID=user;Password=****";
What is the mistake i am doing when i check the connection using TEST BUTTON.. its showing me TEST CONNECTION SUCCEEDED . When i am calling from program its not connection. Why its like that. i am using VS 2013

Placing an # sign before your first sql connection statement will prevent the string from including escape sequences.
string constring = #"Data Source=DVSQL\SQLEXPRESS;Initial Catalog=DB;User ID=user;Password=****";
Maybe something in the password is throwing it off.

Related

Cannot set Connection String to LocalDb using OLEDB

Good morning guys,
I feel quite silly to ask this question, but I have looked everywhere and possibly at all questions in this matter and could not find a solution that would work for me.
Long story short.
I am using a local database called TestDB.mdf in windows form application. The application is designed to do (as per current) two simple things.
1. Import data from excel document into the database - which I don't have any issues with.
and...
2. Clear all data stored in that database - this is where I am struggling
For the import data into the database (point 1) I am using two connection strings. One for excelConnectionString
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " +
"C:\\Users\\User.AR\\Desktop\\export.xls; " +
"Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";
and second for sqlConnectionString
string sqlConnectionString = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\arkadiusz.rzepka\source\repos\Database_application\Database_application\TestDB.mdf;Integrated Security=True";
Then I use SqlBulkCopy to import all data and all is working like a charm.
Now the issue I can see is that I cannot open a connection to clear all data from the same database.
I have navigated to properties of my database to find connection string and this has been presented in the below format:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User.AR\source\repos\Database_application\Database_application\TestDB.mdf;Integrated Security=True
I have had to amend the above connection string as I was getting errors such as missing provider, should be like Provider=SQLOLEDB, after adding a provider, I have had to change Integrated Security = SSPI, and now I am getting error such as SQL Server does not exist or access denied
My code just to check if the connection was opened is presented below and I would be grateful if you could advise of what I am doing wrong.
private void DeleteAllRecords()
{
string connectionString = #"Provider=Sqloledb;Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User.AR\source\repos\Database_application\Database_application\TestDB.mdf;Integrated Security=SSPI";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
try
{
connection.Open();
MessageBox.Show("Connection openned successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
If you run this in the command prompt.
SqlLocalDB.exe i "MSSQLLocalDB"
You will get the instance pipe name. You should be able to use that in your OLEDB connection. This has worked for me when connecting Excel using OLEDB connection to my MSSQLLocalDB.
Name: MSSQLLocalDB
Version: 13.1.4001.0
Shared name:
Owner: Foo\Foo.Bar
Auto-create: Yes
State: Running
Last start time: 18/03/2021 09:00:34
Instance pipe name: np:\\.\pipe\LOCALDB#DA1FAFF6\tsql\query
So my final connection string in excel looked like this.
provider=SQLOLEDB;initial catalog=IpsosDC;data source=np:\\.\pipe\LOCALDB#DA1FAFF6\tsql\query
Try using double bar on the paths of your connection strings, instead of one. So it would be like this:
string connectionString = #"Provider=Sqloledb;Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\User.AR\\source\\repos\\Database_application\\Database_application\\TestDB.mdf;Integrated Security=SSPI";

Microsoft Visual Studio SQL server connection - invalid pointer

So, I want to create a SQL database in Microsoft SQL Management Studio and connect it to Microsoft Visual Studio. I linked the database to Visual Studio and it worked. Now, I want to open that connection to test it with a button on a windows form application. Every time I try, it says "invalid pointer" but the database name is correct. I don't know what is wrong.
I still get the invalid pointer error...My instance name is DESKTOP-BJSAO6B but it doesnt seem to work...
You need provide a proper connection string like
string connectionString =
"data source=.\SQLEXPRESS;initial catalog=student;integrated security=True;";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
connection.Open();
MessageBox.Show("You are connected");
}
where SQLEXPRESS - name your MSSQL instance ( may be different). More you can see here
For Ex:
string connectionString = "Data Source=Server-Name; Initial Catalog= Database-Name;Integrated Security=True"; //If you are using a local Database.
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
MessageBox.Show("You are connected");

connecting Access DB to VS using C#

This is the 1st time I am using C# connection with db, also I dont use access.
I just want to know the initial steps required to connect the access db to visual studio C# windows application.
I have searched though the internet, It helped a lot. I cant find my mistake in the code. I think I am missing some steps in establishing the connection with the db.
here is the error that appears when I try to run the program "'\f38910\Users\kainat.baig\Desktop\AAA\Database101' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides."
CODE:
OleDbConnection bookConn;
OleDbCommand oleDbCmd = new OleDbCommand();
String connParam = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \\f38910\Users\kainat.baig\Desktop\AAA\Database101; Persist Security Info=False";
public Form1()
{
bookConn = new OleDbConnection(connParam);
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bookConn.Open(); //*ERROR LINE
oleDbCmd.Connection = bookConn;
bookConn.Close();
}
First your file name does not have the file extension name like .mdb
Database101\MyDB.mdb
Second, if it is in a network. Would it be better if you net use first in you DOS prompt to assign a drive letter for your network, like:
net use z: \\f38910\Users\kainat.baig\Desktop\AAA\Database101 your_password /USER:your_account
Then your connection string will be shorter then:
String connParam = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= z:\MyDB.mdb; Persist Security Info=False";
If the file extension by the way is .accdb instead of .mdb it should be:
String connParam = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=z:\MyDB.mdb;;Persist Security Info=False;"
Or simply your path is simply wrong.
Go to windows explorer and just clink on the path where your file is and copy paste it.
You forget to add .mdb after database name or .accdb if you are using Microsoft Office Access 2007 or higher database but, for that you have to change Provider also.
String connParam = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \\f38910\Users\kainat.baig\Desktop\AAA\Database101.mdb; Persist Security Info=False";
Or you can use connection string like this
String connParam = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\f38910\Users\kainat.baig\Desktop\AAA\Database101.mdb;User Id=admin;Password=;"
EDITED:
for ACCESS 2010
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\f38910\Users\kainat.baig\Desktop\AAA\Database101.accdb"

Creating an SQL Server connection string without importing a datasource in C#

I am going off of this tutorial: http://www.dotnetperls.com/sqlclient . Instead of adding a data source and a having visual studio compile my connecting string - I want to do it myself. The reason being is that the database will not always be the same and I want this application to be able to use different databases depending on which I point it to.
So how can I manually create the connection string? I am using SQL Server 2005.
Step 1: Go to connectionstrings.com and find the proper format for your database.
Step 2: Plug in the appropriate values to the connection string.
Step 3: Pass that string to the constructor of SqlConnection.
I would also suggest storing your connection string in your app.config/web.config file. You can then modify them easily if needed. The proper format can be found at MSDN - connectionStrings element. You then change your code to:
SqlConnection sqlConn = new SqlConnection(
ConfigurationManager.ConnectionStrings["ConnStringName"].ConnectionString);
I don't see where the connection string is "compiled".
In the code
SqlConnection con = new SqlConnection(
ConsoleApplication1.Properties.Settings.Default.masterConnectionString)
ConsoleApplication1.Properties.Settings.Default.masterConnectionString is a field and it can be replaced with any other appropriate string.
for SQL Server format of the connection string is
"Data Source = server_address; Initial Catalog = database_name; User ID = UserId; Password = **;"
save this connection string in a string variable and use with connection object.
either way you can add in web.config file.
<ConnectionString>
<add name = "name_of_connecctionString" ConnectionString = "Data Source = server_address; Initial Catalog = database_name; User ID = UserId; Password = ****;" ProviderName = "system.Data.SqlClient"/>
</ConnectionString>
you can change the provider as needed by you.
then in code behind file access this particular connection string using configuration manager.

Error trying to connect to SQL Server using C#

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";

Categories