By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

Problem connecting to sql server on network?

P: 38
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:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.Sql;
  9. using System.Data.SqlClient;
  10. using System.Data.SqlTypes;
  11.  
  12. namespace WindowsApplication4
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             SqlConnection connection = new SqlConnection();
  24.             connection.ConnectionString = "Server=" + textBox1.Text + ";User ID=;PWD=;database=";
  25.             connection.Open();
  26.             SqlCommand thisCommand = connection.CreateCommand();
  27.  
  28.             textBox1.Text = "Connected";
  29.  
  30.             //create dataset to hold rows, columns, etc
  31.             DataSet dataset = new DataSet();
  32.  
  33.             connection.Close();
  34.         }
  35.         private void get_tables()
  36.         {
  37.  
  38.           //     DataTable tables = new DataTable("Tables");
  39.            //     using (SqlConnection connection =
  40.        //new SqlConnection(connectionString))
  41.  
  42.    //SqlCommand command = connection.CreateCommand();
  43.     //command.CommandText = "select table_name as Name from
  44.     //          INFORMATION_SCHEMA.Tables where TABLE_TYPE =
  45.        //       'BASE TABLE'";
  46.   //  connection.Open();
  47.   //  tables.Load(command.ExecuteReader(
  48.        //             CommandBehavior.CloseConnection));
  49. }
  50.     }
  51. }
  52.  
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
Jul 6 '07 #1
Share this Question
Share on Google+
10 Replies


P: 12
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
Expand|Select|Wrap|Line Numbers
  1. connection.ConnectionString = "Server=" + textBox1.Text + ";User ID=;PWD=;database=";
Jul 6 '07 #2

P: 38
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
Expand|Select|Wrap|Line Numbers
  1. 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=password" and it still didn't work

Am I doing something wrong?
Jul 9 '07 #3

P: 4
put textBox1.Text = "Connected"; before connection string
Jul 10 '07 #4

P: 4
NOT Necessary to give user name and PW if he already logged in as admin or SA
Jul 10 '07 #5

P: 38
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
Jul 10 '07 #6

P: 12
I want to prompt the user to enter their user name and password. i included this in the connection string "User ID=login; Password=password" 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.ConnectionString = "Server=" + textBox1.Text + ";User
Expand|Select|Wrap|Line Numbers
  1. ID=MyUserID;PWD=MyPassword;database=MyDatabase"; 
If you want to make it interactive you need something like
Expand|Select|Wrap|Line Numbers
  1. connection.ConnectionString = "Data Source=" + textBox1.Text + ";User ID=;" + txtUserID.text + "PWD=" + txtPassword.text + ";database=" = + txtDatabase.text + ""; "
Jul 10 '07 #7

P: 38
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.ConnectionString = "Server=" + textBox1.Text + ";User
Expand|Select|Wrap|Line Numbers
  1. ID=MyUserID;PWD=MyPassword;database=MyDatabase"; 
If you want to make it interactive you need something like
Expand|Select|Wrap|Line Numbers
  1. 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?
Jul 10 '07 #8

P: 12
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.
Expand|Select|Wrap|Line Numbers
  1.         Dim m_connectionString As String
  2.         If m_connectionString Is Nothing Then
  3.             m_connectionString = "Data Source=" + txtServerName.text + ";"
  4.             m_connectionString += "Initial Catalog=" + txtDbName.text + ";"
  5.             m_connectionString += "User ID=" + txtUserID.text + ";"
  6.             m_connectionString += "Password=" + txtPassword.text + ";"
  7.         End If
  8.  
"Data Source=localHost;Initial Catalog=Northwind;UserID=clanguage;Password=enter; "
Jul 11 '07 #9

P: 5
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?
Jul 12 '07 #10

P: 38
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

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?
Jul 12 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.