how to update single database from two computers - c#

I want to know how to update a database installed on two computers. I want to update only a single database. Whether I work on 1st computer or 2nd computer I want to update the database once. How can I do this?
This is my database connect method:
public static SqlConnection connect() {
String conct = #"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\3000Apliance\3000Apliance\300Apliance.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(conct);
con.Open();
return con;
}

Use one Computer as Server keep your database there. on other computer use sqlmanagement and connect to server computer via Ip/name 192.0.0.0/name and create a path to database.
this way you can use more then 2 computers to update database at a time. you can connect computers by peer to peer or use a switch to connect multiple computers.
your new connection string should look like this:
Data
Source = . \ SQLEXPRESS; AttachDbFilename=192.0.0.0\name\3000Apliance\3000Apliance\300Apliance.mdf; Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(conct);

Related

How to connect an application to a SQL Server using IP Adress in C#?

My problem is that I have created a SQL Server database but I access it locally, only from my own computer. What I want to do is to connect the app to a server and to save all the data there.
The code I used to connect to the SQL Server database is:
SqlConnection con = new SqlConnection(#"Data Source=(localdb)\v11.0;AttachDbFilename=C:\Users\donca\Desktop\Memo\Memo\ContNou.mdf;Integrated Security=True");
SqlCommand cmd1 = new SqlCommand("SELECT * FROM [dbo].[Cont] WHERE Nume_utilizator = #Nume_utilizator and Parola = #Parola;", con);
cmd1.Parameters.AddWithValue("#Nume_utilizator", this.Nume_utilizator.Text);
cmd1.Parameters.AddWithValue("#Parola", this.Parola.Text);
cmd1.Connection = con;
con.Open();
My question is: how can I change this to connect and access data from a SQL Server and save data there?
Just change the connection string:
"Data source=ServerName\InstanceName;"
You can use the IP address instead of the servername.
The default instance name is MSSQLSERVER.
Make sure you turn on "Remote connections" on the distant server. You can use Management studio -> Server properties -> connections -> Allow remote connections.
If it dosn't work check that the TCP/IP protocol is activated for you instance.
You can find it in SQl Server Configuration Manager.
You just have to change your connection string. That's all. For an example, if your server name is CORP and the SQL instance name is SQL2012, your connection string will be like this.
SqlConnection con = new SqlConnection(#"Data Source=CORP\SQL2012;Initial Catalog=ContNou;Integrated Security=True");
In case if you cannot find the SQL server by its name, you can use the IP too.
SqlConnection con = new SqlConnection(#"Data Source=10.4.2.208;Initial Catalog=ContNou;Integrated Security=True");

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

can't connect to database in second form c#

I am trying to connect database in project have multiple windows form connection to the first form to database is working fine but when is try to access database inside second form i get error
An attempt to attach an auto-named database for file c:\Users\username\AppData\Roaming\mydatabsename.mdf failed.A database with same name exist,or specified file cannot be opened,or it is located on UNC share.
Connection in first form is
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\BbCon.mdf;Integrated Security=True;Connect Timeout=30;");
I use same connection in second form
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\BbCon.mdf;Integrated Security=True;Connect Timeout=30;")
Please help me

Connect to online MySQL database in C# desktop application

I have a problem with connecting to the database.
private static readonly string s_connectionString = "Server=db.inu.hu; Database=patientRegistry;UID=****; Password=****; Port=3306; Trusted_Connection=true";
That's my connection string and with this I can't open the connection.
using (SQLiteConnection conn = new SQLiteConnection(s_connectionString))
{
conn.Open();
}
I thought the problem might be that I have a table in the database and need to add that to the connection string.
Are you trying to connect to MySQL or SQLite? You are referencing a SQLiteConnection object, but I think you need to be using a MySqlConnection connection object instead.

How to connect to local database and update a dataset in C#?

I have stored some products (a product contains: unique id - primary key, name, price, quantity) in a local database file (Stock.mdf, table for the products is called 'table').
The file is in my project folder in Visual Studio. When someone call an action in the form (WPF), a entry should be updated (new quantity).
In my C# code file i use the following code to connect to the database:
System.Data.SqlClient.SqlConnection con;
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = #"Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Stock.mdf; |
Integrated Security=True;
Connect Timeout=30;
User Instance=True";
//Do i need the connect timeout? Found it somewhere in the web
I know that I can update the table in SQL with this command:
UPDATE table
SET Quantity=newQuantity
WHERE Id=GivenId;
How can I connect to the local dataset and update a product with the new quantity in c#?
First of all, please make use of using statements. They will ensure that IDisposable.Dispose() is called.
Written from memory:
string cs = #"Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Stock.mdf; |
Integrated Security=True;
Connect Timeout=30;
User Instance=True";
using (SqlConnection con = new SqlConnection(cs))
using (SqlCommand cmd = new SqlCommand("UPDATE table SET Quantity=#q WHERE Id=#Id", con))
{
cmd.Parameters.AddWithValue("#q", newQuanity);
cmd.Parameters.AddWithValue("#Id", GivenId);
con.Open();
cmd.ExecuteNonQuery();
}
Do i need the connect timeout? Found it somewhere in the web
The default is fine for most circumstances.
Note that it is better practice to place the connection string in your app.config (or web.config, for web apps).
<connectionStrings>
<add name="MyConnectionName" connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Stock.mdf; |
Integrated Security=True;
Connect Timeout=30;
User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Then access it like:
string cs =
ConfigurationManager.ConnectionStrings["MyConnectionName"].ConnectionString;
To connect to the Sql database using C# in Microsoft Visual studio,you have to manually add the System.Data.SqlSeverCe.dll name reference as follow:
Right-click on your Project >>> Add Reference
Browse >>> C:\Program Files\Microsoft SQL server Compact edition\v3.5\Desktop
Load >>> the System.Data.SqlSeverCe.dll >>> Add

Categories