How to run the same C# windows application on 2 computers

NadOke 41 Reputation points
2021-06-20T16:02:42.9+00:00

I have 2 PCs.
Let them be pc1 and pc2.

pc1 has Microsoft Visual Studio and SQL server management studio.
I have created a C# windows app form to insert applicant info into a SQL table in pc1. These two are connected "using System.Data.SqlClient" and runs perfectly on pc1.

pc2 only has SQL server management studio, it does not have Microsoft Visual Studio.
The same database and the relevant table is created in pc2.

I want to run my application on pc2 and connect it with the database in pc2.

using System.Data.SqlClient;

    namespace Officer_Cadet
    {
        public partial class Candidates : Form
        {
            public Candidates()
            {
                InitializeComponent();
            }

            SqlConnection con;
            SqlCommand cmd;

            private void Candidates_Load(object sender, EventArgs e)
            {
                con = new SqlConnection("Data Source=.;Initial Catalog=Officer__Cadet;Integrated Security=True");
            }

            private void btn_submit_Click(object sender, EventArgs e)
     {
                        con.Open();
                        cmd = new SqlCommand("Insert into CandidateInformation values('" + txt_name.Text + "', '" + txt_address.Text + "', '" + txt_tele1.Text + "', '" + txt_tele2.Text + "', '" + txt_nic.Text + "', '" + txt_mail.Text+ "','" + gender + "' , '" + ch1 + "', '" + ch2 + "', '" + ch3 + "')", con);
                        cmd.ExecuteNonQuery();
                        con.Close();

                        MessageBox.Show("Successfully Registered", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

The above is the code I've used to connect the C# program to the database in pc1.

I tried changing the data source from "." to the server name of pc2 but it didn't work.

I will have to put the files in a pen drive and move to pc2. When I submit the information then it should store in the database in pc2 with the same name.

Please please help me. I am earnestly seeking help.
Thank you in advance.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,714 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 101K Reputation points MVP
    2021-06-20T21:02:42.487+00:00

    You say that PC2 has SQL Server Management Studio. But do you have SQL Server installed on the machine? SSMS is not SQL Server. It is just a tool that permits you to talk to SQL Server.

    While unrelated to your question, I like to point out that this is very wrong:

    cmd = new SqlCommand("Insert into CandidateInformation values('" + txt_name.Text + "', '" + txt_address.Text + "', '" + txt_tele1.Text + "', '" + txt_tele2.Text + "', '" + txt_nic.Text + "', '" + txt_mail.Text+ "','" + gender + "' , '" + ch1 + "', '" + ch2 + "', '" + ch3 + "')", con);
    

    This should be:

    cmd.CommantText =@ "Insert into CandidateInformation (col1, col2, col3, ...)
        VALUES(@name, @address,  ...)
    cmd.Parameters.Add("@name", SqlDbType.NVarchar, 30).Value = txt_name;
    cmd.Parameters.Add("@address", SqlDbType.NVarChar, 50).Value = txt_address;
    ...
    

    Building a string of input values comes with a lot of problems:

    1. It is difficult to get write and to read.
    2. Open for SQL injection.
    3. It prevents SQL Server from caching an execution plan and can cause performance issues.

    Using parameterised commands is the way you must use. The actual data type depends on the column. The numbers 30, 50, in the example is the length of the target columns.


1 additional answer

Sort by: Most helpful
  1. Alberto Poblacion 1,556 Reputation points
    2021-06-21T06:03:32.837+00:00

    Yes, the idea of using "." to indicate the Data Source is correct. That should cause the program to connect to the default instance of SQL Server that is running locally on the same machine.

    If it is not working for you, it would be helpful to see the exact error message that it is returning. Just saying "it doesn't work" is not very helpful for resolving the problem. I suspect that the issue may be that you do not have a default instance of SQL Server in that machine. If you installed a named instance, then you need to append the instance name after the dot, such as ".\sqlexpress", but of course writing the actual instance that you installed instead of "sqlexpress".
    And another issue that could happen is that the instance does not contain the database "Officer__Cadet" specified in your connection string. This will not happen automatically just by copying your files; you need to use the management tools to attach it. Or, alternatively, you could use the database in "user instance" mode. This will work by just copying the files, but it requires a connection string that is different from the one that you have.

    0 comments No comments