473,378 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:

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
10 1510
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
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
put textBox1.Text = "Connected"; before connection string
Jul 10 '07 #4
NOT Necessary to give user name and PW if he already logged in as admin or SA
Jul 10 '07 #5
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
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
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
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
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
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

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

Similar topics

12
by: Ann Marinas | last post by:
Hi all, I would like to ask for some help regarding separating the asp.net webserver and the sql server. I have created an asp.net application for a certain company. Initially, we installed...
2
by: John | last post by:
Hi I was working fine with create user wizard and the default membership provider. I have now customised the membership provider as per attached web.config. The create user wizard picks up the...
0
by: mortenol | last post by:
Hi, I am trying to connect a MS SSIS package to an AS400/DB2 database, and I experience problem when I hit the "Create Package" button in the "Data Link properties window". I have understood that...
0
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
0
by: NoaGross | last post by:
Hi, I'm relly new in java and I have a problem. I'm using java applet. When using http all ok, but when trying to use https i get: Java Plug-in 1.5.0_10 Using JRE version 1.5.0_10 Java...
1
by: Vikram S | last post by:
Hi All, I have an ASP.NET 2.0 web application on a web server and a separate machine for SQL Server 2000 Database. I am using a Connectionstring based on Sql Authentication to connect to the...
8
by: JDavis | last post by:
I am using System.Net.Sockets to connect a client socket to a server that requires three inputs when I connect: host, port and an identification number that identifies the person connecting. ...
2
by: orandov | last post by:
Hi, I am having a problem connecting my .net applications from the application server to the database server. When I run the application from my windows xp (sp2) box it works fine. When I try to...
0
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.