Results 1 to 4 of 4

Thread: how to attach database into .exe windows application?

  1. #1
    Addicted Member
    Join Date
    Mar 07
    Posts
    200

    how to attach database into .exe windows application?

    I am developing a windows application project using C# , MS sql 2005 and VS 2008.
    now i want to run the application in other computers ( visual studio and MS sql server are not installed in these client computers).
    i can run the applications interface bu taking the .exe file inside bin. But i cannot run it properly b/c the database is not there.
    so how can i make it to run properly.. without installing the ms sql server on other computers.
    any tools or configuration to embed the database along with windows application in .exe file please?
    thanks

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,793

    Re: how to attach database into .exe windows application?

    No. If you want to use SQL Server then you need SQL Server installed on the user's machine or somewhere that they can access it. If you don't want to require a database server then don't use a server-based database. Use a file-based database like SQL Server CE, Access or SQLite. If you want to stick with SQL Server then you can use SQL Server Express. It's free, it allows you to include the data file as part of your project and distribute it with your app and it can be installed, and even downloaded if required, automatically as part of your installation.

  3. #3
    Addicted Member
    Join Date
    Mar 07
    Posts
    200

    Re: how to attach database into .exe windows application?

    thank you. i did like below. i detached the database then add to my project under db folder . i use the following code
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace winform
    {
        public partial class Form1 : Form
        {
            SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testdbConnectionString"].ConnectionString);
            
               
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string sqlrooms;
                SqlCommand addroomcmd=new SqlCommand();
                sqlrooms = "insert into testtbl(fname,lname)";
                sqlrooms += string.Format("values(@fname,@lname)");
                addroomcmd = new SqlCommand(sqlrooms, dbcon);
                addroomcmd.CommandType = CommandType.Text;
                
                addroomcmd.Parameters.AddWithValue("@fname", (string)textBox1.Text);
                addroomcmd.Parameters.AddWithValue("@lname", (string)textBox2.Text);
                
                dbcon.Open();
                addroomcmd.ExecuteNonQuery();
                dbcon.Close();
                MessageBox.Show("added successfully");
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                
    
            }
            public void listnames()
            {
               dataGridView2.DataMember = "testtbl";
              dataGridView2.Columns[0].HeaderText="ID";
               dataGridView2.Columns[1].HeaderText = "Fname";
              dataGridView2.Columns[2].HeaderText = "Lastname";
               
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string sqlfloor;
                sqlfloor = "select idno, fname,lname from testtbl";
    
                SqlDataAdapter adapter = new SqlDataAdapter(sqlfloor, dbcon);
                dbcon.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds, "testtbl");
                dataGridView2.DataSource = ds;
                this.listnames();
                dbcon.Close();
    
            }
        }
    }
    the app.config is before detached the databse it was like this
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
      </configSections>
      <connectionStrings>
      
        <add name="testdbConnectionString" connectionString="server=localhost;user id=sa;password=12345;database=testdb"
          providerName="System.Data.sqlclient" />
        
      </connectionStrings>
    </configuration>
    upto this everything is work fine..in my computer. now i want to take the project into other clients so i detached the database and then change my app.config connection like this one



    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
      </configSections>
      <connectionStrings>
        <add name="winform.Properties.Settings.testdbConnectionString"
          connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db\testdb.mdf;Integrated Security=True;User Instance=True"
          providerName="System.Data.SqlClient" />
     
        
      </connectionStrings>
    </configuration>
    now it gives me error
    NullReference Exception was unhandle
    Object reference not set to an instance of an object.
    use the "New" keyword to create an object instant

    please help.. thanks

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,793

    Re: how to attach database into .exe windows application?

    Shall we play a game where we guess where the exception occurred? Alternatively, you could tell us.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •