Inability to connect with database - c#

I use Visual Studio 2012. I created a database using the "Add component" function. I try to connect to it
class DBManager
{
private SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\Users\\somepath\\Database1.mdf;Integrated Security=True");
public DBManager()
{
using (conn)
{
conn.Open();
}
}
And I get SQL exception on the conn.Open() line. Even after trying to use System.Data.SqlServerCe and changing connection string accordingly it still throws the same exception about file being impossible to open or damaged.

If you are using SQL server your connection string should look like this:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
Go to connectionstrings.com for more details -- or if you are using a different DB.

If the database you're trying to attach it's already attached, the parameter AttachDbFilename=C:\\Users\\somepath\\Database1.mdf in your connection is useless. Removing it, you will connect to the default database of your instance.
If you're receiving an error because your file is impossible to open or it is damaged, check its integrity, attaching the file using SQL Server Management Studio and change mdf file. If the file has some errors, your code won't work anyway.

Related

Attaching SQL database behaviors

I am trying to create a setup.exe that installs an application I've written.
The application uses a database to store and retrieve information.
In the setup, I have the installing user supply a Database server\instance, credentials and the DATA directory to copy the database files to.
I want to programmatically and permanently attach the database.
I use the following to programmatically attach a database to my instance.
The con.Open() and con.Close() are just for testing.
System.Data.SqlClient.SqlConnection con;
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = #"Data Source=.\SQLEXPRESS;Integrated Security=true;Connect Timeout=30;User Instance=True";
ServerConnection serverconn = new ServerConnection(con);
Server s = new Server(serverconn);
s.DetachDatabase("DBX2", true, true);
s.AttachDatabase("DBX2", new System.Collections.Specialized.StringCollection { #"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\DBX2.mdf", #"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\DBX2_log.ldf" }, AttachOptions.None);
con.Open();
MessageBox.Show("Connection Opened");
con.Close();
MessageBox.Show("Connection Closed");
Several questions to just confirm:
I get an error that it is already open, when it tries con.Open(), so does attaching a database this way automatically open it? When I remove the con.open(), it runs through and closes it just fine.
When I attach my database this way, I do not see it attached in the SQL Server Management Studio, like if I were to manually attach it there. So is the database still attached and working after I exit my application, so that other applications can access it? Is attaching this way temporary, or is it just not picked up by the SQL Management Studio?
Is this the correct way to go about achieving what I want?
Each application has it's own independent 'connection string' that provides a way to connect to the same database. SSMS and other client executables are just more of these applications. Of course, each connection string could actually be the same thing as they all point to the same database, they are just stored differently.

windows forms connect to local database

I have created a very basic windows form application using visual studio 2015.
A service based database has been added (mdf database), and a table has been created..
The database has been added to the service explore, and i can see the connection string in the setting file.
I am simple trying to open a connection to the database to be able to write to it.. Cant seem to find any basic example..
Tried to use this :-
using (var con = new
SqlConnection(Properties.Settings.Default.MyConnectionString))
{
con.Open();
}
}
but no luck. Seem to get the following error
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."}
my connection string :-
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirector‌​y|\Data\mydb.mdf;Int‌​egrated Security=True

Make a database connection to Sage Abra Suite

I want to create a CSV file from our Sage Abra Suite database using C#. At this point I am just trying to make a connection to it and having difficulty to create one and to test it.
I have a Visual Studio 2010 installed, and the table that I want to access is resided on the server (called it Server1). As far as I know the database that the Sage Abra uses is VisualFoxPro
So I need help right now to how to create the database connection and testing the connection in C#.
Ended up using a connection string
// Build the connection string
string connectionString = #"Provider=VFPOLEDB.1;Data Source= myServer;Collating Sequence=general";
// Create the connection
OleDbConnection connection = new OleDbConnection(connectionString);
// Open the connection
connection.Open();

Problem In Open Or Use master.mdf database in C#

I define Sql_Cmd And Other sql variable that need before
and Now write this code:
string strConnection2 = "Data Source=.\\sqlexpress;AttachDbFilename=master.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection sqlcon2 = new SqlConnection(strConnection2);
string sql = "select * ";
Sql_Cmd.CommandType = CommandType.Text;
Sql_Cmd.CommandText = sql;
Sql_Cmd.Connection = sqlcon2;
try
{
sqlcon2.Open();
Sql_Cmd.ExecuteNonQuery();
}
catch (SqlException Error_Exception)
{
//FormError1 = new FormErrorInDataBase();
//FormError1.Show();
}
When I want to open sqlcon2 I see this error:
An attempt to attach an auto-named database for file master.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Please help me - how can I open master database and then use select query for this database, and what is the connection string for master.mdf?
Can I write the directory of mater.mdf such as C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\master.mdf or is it enough that I write AttachDbFilename=master.mdf?
Are you trying to open 'the' master database? The system database called 'master' is used internally by SQL Server and should not / does not need to be opened or attached in this way. If not, you'll have to call your database something other than 'master' - that is a reserved database name.
Check your parameters again. The right syntax to add an .mdf-file is as follows. Attach a database file on connect to a local SQL Server Express instance:
Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.
See: connectionstrings.com
master database is the system database that contains system information about SQL Server instance, information about databases contained in this instance and so on. This database is opened automatically when SQL Server starts.
So, when you want to connect to SQL Server, master database is already exist and cannot be attached. Information about this database you can see here
I cannot understand why you need to attach this database. Maybe you don't need to attach this database, but only connect to it? If so, you need to change th code:
string strConnection2 = "Data Source=.\\sqlexpress;initial catalog=master;Integrated Security=True;Connect Timeout=30;"

SQL Server CE Database Connection Issues

I have an application with a database called voodoobase.sdf.
Using .NET Framework Data Provider for Microsoft SQL Server Compact 3.5
I can see it in Server Explorer and connect to it fine from there. The DB File is located in:
c:\Users\me\Documents\VisualStudio2010\Projects\testproj\voodoobase.sdf
The same named DB under Solution Explorer is said to reside at the same location.
c:\Users\me\Documents\VisualStudio2010\Projects\testproj\voodoobase.sdf
Assuming they are the same... why can my application which compiles successfully alwways crash with a connection error:
SqlConnection dbCon = new SqlConnection(Properties.Settings.Default.voodoobaseConnectionString);
dbCon.Open();
Throws an error on dbCon.Open() saying that could not get a connection to the SQL server. Let me know if further detail is required.
Do not use the SqlConnection class, but the SqlCeConnection class.

Categories

Resources