none
Connecting to Database with Server app. RRS feed

  • Question

  • Hi,

     

    I must be missing something very simple here, but can't figure out how to do what I need.

     

    I have finally managed to get a client app to talk to a server DLL using .Net remoting.  What I need is for the server DLL to be able to connect to a SQL Server database and query a table and return a DataSet.

     

    Is there a simple example somewhere (preferably in C#).  I want to do something very simple initially:

     

    Client application has on form with a list box and a button on it. When the button is pressed, the server database should be queried and return a list of users and the list box is populated with this list.

     

    I have managed to get the client app to talk to an object on the server and can return simple text strings. However, I need my sever classes to connect to a database and return data.  Ideally, I wanted to use config file to store the location of the database so that it would be easy to point to a different database, but I am willing to hard-code the connection in the class now.

     

    I seem to be unable to connect to the database on the server at all. maybe I have not set something up properly. I am using SQL Sever Express 2005.

     

    Any help is much appreciated.

     

    Pete

    Tuesday, July 10, 2007 2:38 PM

Answers

  • if the file is not on the Server (remoting server) and exist on somewhere else, then its network permission problem. i know for sql server that you have to give remote access permission. check for Access. Its not remoting issue.

    Thanks

     

    Wednesday, July 11, 2007 1:07 PM

All replies

  • Put this  function in Server.cs and publish this,Let client use this function.

    public DataSet getUsers()

    {

    string connectionString= "put any conn string";

    SqlConnection con = new SqlConnection(connectionString);

    con.open();

    SqlDataAdapter adapter = new SqlDataAdapter (con,"Select users from userTable");

    Dataset ds = new Dataset();

    adapter.Fill(ds);

    return ds;

    }

     

     

    Tuesday, July 10, 2007 3:08 PM
  • Thanks for that.  My problem is what to put in for the connection string.  I suspect that there may be a permissions problem with the client application connecting to the database on the server.  I can't believe that this is so difficult.  Are there no simple examples anywhere? All of the examples on .Net Remoting simply have the remote object returning a simple string (which I have managed to do).  It would be helpful to get an example that does something with a database table.

     

    Pete

    Tuesday, July 10, 2007 3:39 PM
  • just put any connection string. There is nothing to suspect. If it wont run then paste ur code here and we will help u.
    Tuesday, July 10, 2007 5:12 PM
  • Moved from the remoting forum
    Tuesday, July 10, 2007 8:41 PM
  • what happend?
    Tuesday, July 10, 2007 9:01 PM
  • I have a class called clsConnection with a cnnString property:

     

    public String cnnString

    {

        get

        {

            //String strConn = "Data Source=.\\SQLEXPRESS;

              AttachDbFilename=\\\\OmegaServer\\Omega Database\\Omega\\Omega.mdf;

              Integrated Security=True;Connect Timeout=30;User Instance=True";

            String strConn = "Server=OmegaServer;Data Source=.\\SQLEXPRESS;AttachDbFilename=N:\\Omega\\Omega.mdf;

               Integrated Security=True;Connect Timeout=30;User Instance=True";

           //String strConn = "Server=OmegaServer;Database=d:\\Omega Database\\Omega\\Omega.mdf";

          return strConn;

        }

    }

     

    The commented out lines are different options that I have tried.

     

    Then in another class (clsUser) I have another method which uses the clsConnection class to try to connect to the database:

     

    public void PopulateUserList(DataSet pdsUserSet)

    {

        clsConnection ccConn = new clsConnection();

        SqlConnection sqlConn;

        String strConn;

     

        strConn = ccConn.cnnString;

        if (strConn != null)

        {

            sqlConn = new SqlConnection(strConn);

            try

            {

              sqlConn.Open();

            }

            catch

            {

              Console.WriteLine("Could not open connection.");

            }

            SqlDataAdapter sqlCommand;

            sqlCommand = new SqlDataAdapter("spGetUserList", sqlConn);

            sqlCommand.SelectCommand.CommandType = CommandType.StoredProcedure;

            // TotalRowsNum

            SqlParameter TotalRowsNum = new SqlParameter("@TotalRowsNum", SqlDbType.Int);

            TotalRowsNum.Value = null;

            TotalRowsNum.Direction = ParameterDirection.Output;

            sqlCommand.SelectCommand.Parameters.Add(TotalRowsNum);

            sqlCommand.SelectCommand.Parameters["@TotalRowsNum"].Value = null;

            sqlCommand.Fill(pdsUserSet, "users");

            sqlConn.Close();

        }

    }

     

    when trying the sqlConn.Open(); command I get the error:

     

    The file \"N:\\Omega\\Omega.mdf\" is on a network path that is not supported for database files.\r\nAn attempt to attach an auto-named database for file N:\\Omega\\Omega.mdf failed.  A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

     

    Pete

     

    Wednesday, July 11, 2007 8:05 AM
  • if the file is not on the Server (remoting server) and exist on somewhere else, then its network permission problem. i know for sql server that you have to give remote access permission. check for Access. Its not remoting issue.

    Thanks

     

    Wednesday, July 11, 2007 1:07 PM
  • The file is definitely on the server. I suspect that it is a remote access permission problem, but I think that I have grantedt access.  I'll have to dig a bit further into  SQL Server issues.

     

    Pete

    Wednesday, July 11, 2007 2:07 PM
  • I have finally managed to get a connection and retrieve data from the database on the server.  I had to create the database on the server from Visual Studio .Net 2005 on my client PC.  So I guess it is a permissions problem.

     

    Pete

    Thursday, July 12, 2007 7:31 AM