Problem connecting to sql server on network?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lildiapaz
    New Member
    • Jul 2007
    • 38

    #1

    Problem connecting to sql server on network?

    Hi, Everyone

    I am trying to connect to an SQL 2000 server in c# using a windows application. What I'm trying to do is ask the user to type in the server name and from the user's input take that server name that they enter in and connect to that server. i also want them to enter a user name and password (SQL authentication) and hide their connection string. Here is my code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    
    namespace WindowsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = "Server=" + textBox1.Text + ";User ID=;PWD=;database=";
                connection.Open();
                SqlCommand thisCommand = connection.CreateCommand();
    
                textBox1.Text = "Connected";
    
                //create dataset to hold rows, columns, etc
                DataSet dataset = new DataSet();
    
                connection.Close();
            }
            private void get_tables()
            {
                
              //     DataTable tables = new DataTable("Tables");
               //     using (SqlConnection connection =
           //new SqlConnection(connectionString))
    
       //SqlCommand command = connection.CreateCommand();
        //command.CommandText = "select table_name as Name from
        //          INFORMATION_SCHEMA.Tables where TABLE_TYPE =
           //       'BASE TABLE'";
      //  connection.Open();
      //  tables.Load(command.ExecuteReader(
           //             CommandBehavior.CloseConnection));
    }
        }
    }
    Can some please help me, I don't understand why I can't connect to the sql server when the user provides the server information?

    Thanks
    Last edited by RedSon; Jul 6 '07, 08:35 PM. Reason: Please use CODE tags in the future.
  • Clanguage
    New Member
    • Jun 2007
    • 12

    #2
    I see that you enter the Server name via the texbox but I do not see any UserID or PWD being passed to the connection string
    Code:
    connection.ConnectionString = "Server=" + textBox1.Text + ";User ID=;PWD=;database=";

    Comment

    • lildiapaz
      New Member
      • Jul 2007
      • 38

      #3
      Originally posted by Clanguage
      I see that you enter the Server name via the texbox but I do not see any UserID or PWD being passed to the connection string
      Code:
      connection.ConnectionString = "Server=" + textBox1.Text + ";User ID=;PWD=;database=";
      I want to prompt the user to enter their user name and password. i included this in the connection string "User ID=login; Password=passwo rd" and it still didn't work

      Am I doing something wrong?

      Comment

      • ahmadmsc
        New Member
        • Jul 2007
        • 4

        #4
        put textBox1.Text = "Connected" ; before connection string

        Comment

        • ahmadmsc
          New Member
          • Jul 2007
          • 4

          #5
          NOT Necessary to give user name and PW if he already logged in as admin or SA

          Comment

          • lildiapaz
            New Member
            • Jul 2007
            • 38

            #6
            Originally posted by ahmadmsc
            NOT Necessary to give user name and PW if he already logged in as admin or SA
            What do you mean? I don't want the user to login under sa or admin. How do I get around this issue and prompt them for user name and password while still using sql authentication?

            I'm still confused

            Comment

            • Clanguage
              New Member
              • Jun 2007
              • 12

              #7
              Originally posted by lildiapaz
              I want to prompt the user to enter their user name and password. i included this in the connection string "User ID=login; Password=passwo rd" and it still didn't work

              Am I doing something wrong?
              Hello lildiapaz,
              I was never notified that I had an answer waiting for my reply to you. I will have to check the board settings so that I get a warning when someone replies.
              Can you clarify your situation?
              Are you saying that when you hardcode the username and the password you do not get a connection. Like
              connection.Conn ectionString = "Server=" + textBox1.Text + ";User
              Code:
              ID=MyUserID;PWD=MyPassword;database=MyDatabase";
              If you want to make it interactive you need something like
              Code:
              connection.ConnectionString = "Data Source=" + textBox1.Text + ";User ID=;" + txtUserID.text + "PWD=" + txtPassword.text + ";database=" = + txtDatabase.text + ""; "

              Comment

              • lildiapaz
                New Member
                • Jul 2007
                • 38

                #8
                Originally posted by Clanguage
                Hello lildiapaz,
                I was never notified that I had an answer waiting for my reply to you. I will have to check the board settings so that I get a warning when someone replies.
                Can you clarify your situation?
                Are you saying that when you hardcode the username and the password you do not get a connection. Like
                connection.Conn ectionString = "Server=" + textBox1.Text + ";User
                Code:
                ID=MyUserID;PWD=MyPassword;database=MyDatabase";
                If you want to make it interactive you need something like
                Code:
                connection.ConnectionString = "Data Source=" + textBox1.Text + ";User ID=;" + txtUserID.text + "PWD=" + txtPassword.text + ";database=" = + txtDatabase.text + ""; "
                It's ok, thanks for the response. My situation is that i want to make my windows c# application as interactive as possible. like I mentioned above the problem that I am having is when I specify a server on the network, it does not connect to anything. I placed the code in a try an catch to see what it was doing and it never connects to the server nor does it prompt me for a user name or password.

                Do you think there could be something wrong with my sql 2000 settings that is not allowing me to connect to server/database?

                Can anyone help?

                Comment

                • Clanguage
                  New Member
                  • Jun 2007
                  • 12

                  #9
                  Once again I was not notified of your reply.

                  The server will not prompt you. You need to build the string yourself. Create 4 textboxes and populate them with your server name, Database Name, User Name and password. You should see the result like the string I included at the foot of this post. this should take care of your connection string.
                  Code:
                          Dim m_connectionString As String
                          If m_connectionString Is Nothing Then
                              m_connectionString = "Data Source=" + txtServerName.text + ";"
                              m_connectionString += "Initial Catalog=" + txtDbName.text + ";"
                              m_connectionString += "User ID=" + txtUserID.text + ";"
                              m_connectionString += "Password=" + txtPassword.text + ";"
                          End If
                  "Data Source=localHos t;Initial Catalog=Northwi nd;UserID=clang uage;Password=e nter;"

                  Comment

                  • prpleprncs
                    New Member
                    • Jul 2007
                    • 5

                    #10
                    Are they logging onto a domain. If so, you can add all the users in the OU to SQL and allow the users to log in using Windows Authentication.

                    Even if you use the textboxes with SQL Authentication, you are still going to have to manage the users within SQL.

                    Have to created the users and given them access to the database?
                    Last edited by prpleprncs; Jul 12 '07, 03:08 AM. Reason: typo

                    Comment

                    • lildiapaz
                      New Member
                      • Jul 2007
                      • 38

                      #11
                      Sorry, I still don't understand how to do that, can you send me some example code in c# or refer me to a tutorial? I want them to connect to servers locally and on the network. I want them to connect using sql authentication if possible.

                      Thanks, for the reply

                      Originally posted by prpleprncs
                      Are they logging onto a domain. If so, you can add all the users in the OU to SQL and allow the users to log in using Windows Authentication.

                      Even if you use the textboxes with SQL Authentication, you are still going to have to manage the users within SQL.

                      Have to created the users and given them access to the database?

                      Comment

                      Working...