Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my simple windows form. Here i have one text box and one button.

i have create DB in SQL server 2008. Data base name Test and Table name is sample. Column name is Value.

My coding is
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Test
{
    public partial class Form1 : Form
    {
        SqlCommand cmd;
        SqlConnection cn;
        public Form1()
        {
            InitializeComponent();
            cn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                cn.Open();
                cmd = new SqlCommand("insert into Sample(Value) values('" + textBox1.Text + "')", cn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Success");
                cn.Close();

            }
            catch
            {
                MessageBox.Show("Error");
            }
        }
    }
}



Now how to declare the Connection in Global?

How to deploy the application with SQL?

Any one can help me?
Posted
Updated 5-Sep-13 5:11am
v2
Comments
Richard C Bishop 5-Sep-13 12:31pm    
I already answered this for you. Do you even know what you mean by Global?
Vijaydhas 5-Sep-13 12:40pm    
If i use 50 forms in my application. For that 50 forms i have to give connection string for all the forms?? if i want to change the server name, Than i have to edit all the 50 forms??
Richard C Bishop 5-Sep-13 12:42pm    
You should not have to do that. The connection string should be stored in the .config file and referenced whenever you need it. If you are using different servers, then a new connection string will have to be used.

The connection string you have now will only work on your computer because it is a local instance of the SQL server. In order for others to use your application, they will either need the same instance installed on their machine or you will need a dedicated server for the database and have the connection string connect to that one.
Vijaydhas 5-Sep-13 13:25pm    
Oh. Ok. Now i got the idea. Thank you. Now how to deploy the app with SQL.

My process is:

Open new Setup Project.

Right Click on File System on Target Mechine-> Add Special Folder -> Program Files Folder.

Now right click on Program Files Folder -> Add -> Folder

Give Re Name for That folder(Sample)

Now File -> Add -> Existing Project -> .csproj file.

Now Project Files Folder -> Sample -> Add -> Project Output -> Primary Output

Now Right Click on Primary Output From Vijay(Active) -> Shortcut to Primary Output From Vijay(Active) two times. And i give name for that 2 shortcuts.

Now i drag and drop the first shortcut into User's Desktop. Another into User's Program Menu.

Now View -> Editor -> User Interface

Now click on Start -> Add Dialog -> Select Licence Agreement

Click right click on Licence Agreement -> Properties window -> Licence File -> Select Licence.rtf(Already created)


Now Build the solution.

Now i finished the deployment. But I did not include my DB into this app. And i did not give icon for the application. Please tell me how to include the DB and how to add icon for the application. Please..
Vijaydhas 5-Sep-13 12:39pm    
If i use 50 forms in my application. For that 50 forms i have to give connection string for all the forms?? if i want to change the server name, Than i have to edit all the 50 forms??

1 solution

Following is one method to retrieve the ConnectionString and save it in your program one time so that all of the forms can access it.

1. Add a Module to your project.

2. In the module, declare a variable to hold the ConnectionString:
Public strConnectionString as String

3. In the Load event of your initial form, get the ConnectionString from the .CONFIG file and save it in strConnectionString

4. Use strConnectionString wherever you create a new SQLConnection

See documentation:
Walkthrough: Using a Configuration File to Define a Data Source[^]
Connection Strings and Configuration Files (ADO.NET)[^]
Walkthrough: Creating a Simple Data Application by Using ADO.NET[^]
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900