Cannot attach the file as database - c#

I've already seen all other post and seems cant help me.
String constr = (#"Data Source=(localdb)\MSSQLLocalDB;
AttachDbFilename= E:\Downloads Mainframe\Compressed\Main Menu\HRDAssets.mdf;
initial catalog=HRDAssets;
Integrated Security=True;");
https://cdn.discordapp.com/attachments/268002637905920001/506753130848059394/unknown.png
When I removed
AttachDbFilename= E:\Downloads Mainframe\Compressed\Main Menu\HRDAssets.mdf;
new problem pops
https://cdn.discordapp.com/attachments/506763196225421312/506763211492556800/unknown.png

Remove this line from the connection string
AttachDbFilename= E:\Downloads Mainframe\Compressed\Main Menu\HRDAssets.mdf;
and Initial Catalog property I suggest to use:
Data Source=(LocalDb)\MSSQLLocalDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\HRDAssets.mdf

Related

sql server database connection not working in c#

I'm trying to connect a table database I created within Visual Studio and used the connection string, but keep getting a bunch of errors? I need to do this multiple times but can't even get the first one to work + im not sure what's wrong
screenshot
this is the line of code I've used:
CSharp SqlConnection Con = new SqlConnection(
#"Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename="C:\Users\scara\Documents\nea2022 database.mdf";
Integrated Security=True;
Connect Timeout=30");
I've connected through the tools tab and "connect to database" and it said that it was successful, so I'm really not sure ://
You should escape the inner quotes inside the connection string.
Use double quotes, for example:
CSharp SqlConnection Con = new SqlConnection(
#"Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=""C:\Users\scara\Documents\nea2022 database.mdf"";
Integrated Security=True;
Connect Timeout=30");
Use second double quotes to escape them inside the verbatim string:
SqlConnection Con = new SqlConnection(
#"Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=""C:\Users\scara\Documents\nea2022 database.mdf"";
Integrated Security=True;
Connect Timeout=30");

Connecting to SQL Server database in ASP.NET

I am trying to connect to a SQL Server database in my website. I have created a database from Add -> Add New Item -> SQL Server database. The name of my database file is database.mdf.
I have created a ConnectionString:
<connectionStrings>
<add name="Khulna_website"
connectionString= "Server=(localDB)\\v11.0;Integrated Security=SSPI;Database=Database.mdf;"
providerName="System.Data.SqlClient" />
</connectionStrings>
My first question is, when I open a database that way, is it necessary to add a connection string? Asking that because I can already see a green connection line on the side of the database.
Then, how do I connect it on my C# code?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
The question is, what do I add on ["RegistrationConnectionString"] part? Should I give the name of my ConnectionString? Am I missing any point here? I am completely new here. Any help would be highly appreciated.
Yes, in order to connect to a database - you do need some form of a connection string - one way or another. It's typically considered a best practice to put those connection strings into a config file, so you can modify it without changing your code.
To retrieve the actual connection string from the config, you need to use the name=.... to you gave it in the config file:
<add name="Khulna_website"
*************** this is the **name** of your connection string
Retrieve it like this:
string conStr = ConfigurationManager.ConnectionStrings["Khulna_website"].ConnectionString;
************** same name again
and then use it to create your connection object to the database:
SqlConnection conn = new SqlConnection(conStr);
Put the name of the connection string which is Khulna_website instead of RegistrationConnectionString

DB Connection Setup with Factory Pattern not returning ConnectionString

I am looking to setup a Factory to get my connection to my database. I thought I had set everything up correctly, but I must be missing something because my ConnectionString is not returning when I use .CreateConnection().
My thoughts are that I missed something in my App.config connection string for this to not be working.
This is my first time trying to do this and I was hoping someone could explain to me what I am missing. Thanks!
Here is my code:
const string SQL_CONNECTION_KEY = "MSDBConnection";
var connString = ConfigurationManager.ConnectionStrings[SQL_CONNECTION_KEY];
var providerName = connString.ProviderName;
var factory = DbProviderFactories.GetFactory(providerName);
var conn = factory.CreateConnection();
var conn does not seem to populate with my App.config connection... Here is my ConnectionString:
<connectionStrings>
<add name="MSDBConnection" connectionString="server=.;database=msdb;user id=UserName;password=Password;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Again, any help would be much appreciated. Thanks!
You're not setting the connection string on your connection. Try this.
conn.ConnectionString = connString.ConnectionString;

Why ConnectionString fails for SqlConnection using MSN example in C#?

I am using the following tutorial example verbatim:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx
The error message is that the connection failed. "Modify it to connect to a Norhtwinf=d database accessible to your system."
string connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost";
SqlConnection northwindConnection = new SqlConnection(connectionString);
northwindConnection.Open();
As far as Northwind Database, I downloaded it from this website and I ran it.
http://www.microsoft.com/download/en/details.aspx?id=23654
Would you be able to tell what am I doing wrong?
Data Source property needs to point to your SQL instance name, and if your SQL instance is the default one.
I know that the next suggestion is a little weird and looks like the same that you are using, but try and let me know what happened:
string connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=.";
note that I've modified the Data Source value from 'localhost' to a (dot).
Make sure the account has access to that database, and try using this connection string:
connectionString="Server=MACHINE-NAME\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;"

Connection String Problem

I am a programmer trying to teach myself C#. I am trying to connect the the Northwind.mdf database in a form. I have used the Database Explorer to attach the database to the form, and the test connection button worked. For the connection string I am using "server=.\\sqlexpress; Trusted_Connection=yes; database=Northwind" This connection fails in SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, connectionString); Google has been no help. Any ideas?
Try this?
Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;
Right click on the connection in the "Database Explorer".
Click on "Properties".
See the "Connection String" in the properties window, with its value on the right.
Is this the same connection string, as the one you posted?
Here is the connection string, I could see with a new mdf file I created
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\kalpesh\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
Assuming that the connection string is correct, you will have to escape it in c# (if it contains any of the characters that requires it. for e.g. the backslash character)
string connectionstring = #"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\kalpesh\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
In your case, it should be the path to the Northwind.mdf located on your machine.
Does this work?

Categories

Resources