3

I have developed an application that have a slider that showing some data from a SQL Database. I'm using Visual Studio 2010 and Microsoft SQL Server 2008.

In fact i don't face any problem with my application when i deploy it an run it on my Personal Computer. The problem occurs when i try it on another machine, and the problem is that the application couldn't be connected the SQL database. I tried to figure out the reason of the problem, so i tried it after its deployment on two machines, one of them have a SQL server installed on it, and the other one don't have the SQL Server. The Application worked perfectly on the machine that have the SQL Server installed on it, and it couldn't be connected to the database on the other machine.

This is the Connection String i have used in my application>>

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\KBank.mdf;Integrated Security=True;User Instance=True"

so if there is a way that could enable me to run the application without needing the machine to have a SQL Server installed, i would be grateful. Thanks

4
  • 1
    Youre looking for an embedded database. Take a look at SqlCE and perhaps SQLite. Commented Aug 17, 2012 at 12:50
  • 1
    Do you need a single database shared between users on the local network, or each users of your application has its own database? Commented Aug 17, 2012 at 12:54
  • @Steve it is the same database shared between users. and it wont be changed. Commented Aug 17, 2012 at 13:51
  • In this case the answer from Daniel Hilgarth is the right one. Of course you need a little administrative work before: Install SqlServer on a server machine inside your local network. Give the right permissions to the client machines and users (firewall, users rights, protocols etc...), create/load the database one time and change the connection string to each local machine. (Remove the AttachDbFileName and use the Initial Catalog) and you are right to go :-) Commented Aug 17, 2012 at 14:00

5 Answers 5

3

You need to change the connection string to specify the computer the database is installed on. Something like the following:

connectionString="Data Source=machine\SQLEXPRESS;AttachDbFilename=|DataDirectory|\KBank.mdf;Integrated Security=True;User Instance=True"

Please note: The database on the other machine needs to be configured properly so it can be accessed from other machines.

Sign up to request clarification or add additional context in comments.

4 Comments

the application will be public, i mean it is not for a local network to know the names of clients machine, is there any way to get the machine name of the users to attach it with the connection string ? and what do you prefer, using SQLite or modifying the connection string as you suggested ?
It totally depends on whether every installation needs its own database or all installations need to share the same database.
It is the same database for all of the application users.
Than you need a central server and the name of the server needs to be configured on each client installation.
2

You need to do one of the following:

  • Include the installation of SQL Server Express on the client's machine as part of the installation process
  • Use a centralized SQL server on a server that you and your clients have access to

There are other data access strategies you could employ, such as serializing the data as XML, or using a different database like SQLite or using web services, but put simply, if SQL Server Express isn't installed on your client's machine, you can't force it to work.

2 Comments

if i used SQLite there wont be necessary to install SQL Server on Client's machines, isn't it?
No. SQLite is a completely separate and different product from SQL Server Express or SQL Server. Your code would need to change, and you'd need to read up on the SQLite documentation, but if you want a small, local database with no setup it's an option. I'd start here decoding.wordpress.com/2009/01/14/using-sqlite-with-net and also google "using sqlite from .net"
1

Your connection string is using localhost which is represented via the . in the connection string. This won't work if the machine does not have SQL Server Installed, therefore you need to change the connection string to the remote machine (The machine with a SQL Server Installation, i.e. Your personal computer).

2 Comments

@Hassan - you need to change the connection string to the IP Address of the machine with SQL Server Installed for example: connectionString="Data Source=0.0.0.1\SQLEXPRESS;AttachDbFilename=|DataDirectory|\KBank.mdf;Integrated Security=True;User Instance=True"
where 0.0.0.1 is the IP address of the machine with SQL Server on.
1

If you do not want to have a SQL Server Express installed, you can go for an embedded SQL server via sql server compact(http://en.wikipedia.org/wiki/SQL_Server_Compact) or alternatively have an actual networked database available.

3 Comments

i have tried SQL Server Compact 3.5 and the same problem occured. the application asked me to install the sql server compact on the users machine..
my connection string is Data Source=|DataDirectory|\KBank.sdf;Encrypt Database=True;File Mode=shared read;Persist Security Info=False
the application works good with my machine but the error occurs on the users machines>> thanks
0

If you using centerl database install in Server computer and other all client computer can access to that sever.

If it's you can doing the following way:

first you change your program to write File DSN to Hard disk at start up of program

Use the following code as sample

 private void MakeDSN()
        {
            try
            {
                if (!System.IO.Directory.Exists(@"C:\OTPub"))
                {
                    System.IO.Directory.CreateDirectory(@"C:\OTPub");
                }

                if (File.Exists(@"C:\OTPub\Ot.dsn"))    //delete ErrorLogFile
                {
                    File.SetAttributes(@"C:\OTPub\Ot.dsn", FileAttributes.Temporary);
                    File.Delete(@"C:\OTPub\Ot.dsn");
                }
                string con = "[ODBC]";
                string driver = "DRIVER=SQL Server";
                string uid = "UID=sa";
                string DB = "DATABASE=OTData";
                string server = "SERVER=10.63.210.111";

                var tw = new StreamWriter(@"C:\OTPub\Ot.dsn", true); // make file in location
                using (tw)
                {
                    tw.WriteLine(con);   //write  dataline
                    tw.WriteLine(driver);
                    tw.WriteLine(uid);
                    tw.WriteLine(DB);
                    tw.WriteLine(server);
                }

                lbserver.Text="LOGIN "+server;
            }
            catch (Exception)
            {
                MessageBox.Show("File DSN Error!");
            }
        }

Enter your server Ip as string server

Now you can use connection in separate class

using System.Data.Odbc;

  class DataBaseConnection
    {
        private OdbcConnection conn1 = new OdbcConnection(@"FILEDSN=C:/OTPub/Ot.dsn;" + "Uid=sa;" + "Pwd=123;"); 

        //insert,update,delete
        public int SetData(string query)
        {
            try
           {
                conn1.Open();
                OdbcCommand command = new OdbcCommand(query, conn1);
                int rs = command.ExecuteNonQuery();
                conn1.Close();
                return rs;
            }
            catch (Exception ex)
            {
                conn1.Close();
                throw ex;
            }
        }

        //select
        public System.Data.DataTable GetData(string sql)
        {
            try
            {
                conn1.Open();
                OdbcDataAdapter adpt = new OdbcDataAdapter(sql, conn1);
                DataTable dt = new DataTable();
                adpt.Fill(dt);
                conn1.Close();
                return dt;
            }
            catch (Exception ex)
            {
                conn1.Close();
                throw ex;
            }
        }
    }

Now you can write following code your different required place for connect to database,

 DataBaseConnection db = new DataBaseConnection();

If you need SELECT query:

DataTable dt = db.GetData("SELECT * From TestTable");

If you need Insert, Update, Delete query

int i=db.SetData("INSERT INTO TestTable(name,address,tel) VALUES (testname,Colombo,0777125896) ");

This method can use connect to database without any configurations.

Important: keep remember delete DSN file when exit application for security purpose.

Hope this will help you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.