I get an error when database is connecting - c#

I have a probleme with the database.
On my computer is working well but when i try to run it from other computer is not working.
This is connection string:
SqlConnection connection = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\OlxBotDataBase.mdf;Integrated Security=True");
I try to put database in %appdata% folder, but is not working or maybe i'm doing it wrong.
This is the error:
EDIT: I installed sql server, but now i get this error:

you can change permission for mdf and log file so that it can be accessed through your application

I found the solution.
I installed sql server express and atached the database and i modify connectionString like this:
connection = new SqlConnection(#"Data Source=localhost\SQLEXPRESS;Database=OlxBotDataBase;Integrated Security=True");
But i want to know if there is any way to automaticaly add database.

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.

Want to insert data into my Local database via a button on the web, but there seems to be an issue with the connection

I created a local database inside visual studio it self (1st time that I've done like that)
I got the following code:
SqlConnection conn = new SqlConnection(#"Data Source=DESKTOP-50MFBOD\Hugo; Initial Catalog=Database_AvaliacaoConnectionString1; Integrated Security=true");
SqlCommand cmd = new SqlCommand();
When I click the insert button, it doesn't insert the data into my GridView and it pops up this error:
Things you should check:
Open SSMS and login with following details:
ServerName: DESKTOP-50MFBOD\Hugo
Authentication: WindowsAithentication
If you are not able to login then probably:
your ServerName is wrong OR
Maybe some problem with your TCP/IP connection from SQL server.
If ServerName is wrong, try to use correct name. You can expand the ServerName dropdown during login to SSMS to find correct instance name.
If problem with TCP/IP you can refer following link to see how to fix it.
https://knowhow.visual-paradigm.com/hibernate/solving-sql-server-connection-problem/

Inability to connect with database

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.

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

Login failed for user. Trying to access a SQL Server Express database

I'm trying to access my SQL Server Express database, but every time I try, I receive this message:
Login failed for this user.
Here is the code:
sqlConnection cs = sqlConnection("Data Source=.\\SQLEXPRESS;
Initial Catalog=MyDatabase#4; Integrated Security=TRUE");
cs.Open();
MessageBox.Show(cs.State.ToString());
cs.Close();
How can I fix this problem?
Try to write your computer name instead of . after "Data Source". For example:
Data Source=Michael-PC\\SQLEXPRESS;Initial Catalog=MyDatabase#4;Integrated Security=True";
The SQL is not able to authenticate your windows user because it may have no access rights on the database... Try using an administrator account or to use a SQL Server privileged account.
What I sometimes do is get Visual Studio to connect, via the Server Explorer, and then copy and paste the connection string from there. If you have SQL Management Studio Express, that will work too.

Categories

Resources