-3

I have developed a program (C#) which creates a SQL database using this code:

string SQLCreation = "IF NOT EXISTS (SELECT * FROM master..sysdatabases WHERE Name = 'x') CREATE DATABASE x";
SqlConnection PublicSQLDBCreationConnection = new SqlConnection(connectionString);
SqlCommand PublicSQLDBCreation = new SqlCommand(SQLCreation, PublicSQLDBCreationConnection);

try
{
   PublicSQLDBCreationConnection.Open();
   PublicSQLDBCreation.ExecuteNonQuery();
   PublicSQLDBCreationConnection.Close();
}
//'then creates a table and so on

Now I want to have a client application which connects to this database (via LAN) WITHOUT using IP or computer name. How is that possible? Is it possible do this and have a dataset while not mentioning IP Adr. or computer name?


P.S. Don't Worry Guys, I simplified my code just for your view, I have made sure that SQL injection or other attempts won't happen. Also I have to say that My Reason for not mentioning servername or IP is that I want to mass deploy my Application on many Networks

BShakiba
  • 13
  • 1
  • 7
  • 11
    How do you tell it where the server is? "Turn left at the first router, then connect to the third CAT-5 jack on the right?" – JNK Jan 26 '12 at 21:28
  • 2
    What do you mean by not connecting without an IP or host name? AFAIK you *must* have one of those as part of the connection string. – blowdart Jan 26 '12 at 21:29
  • It is not possible to connect directly to the database without specifying what machine it is on. Depending on your requirements, you could do some indirection using a service layer or something; what is the requirement/reason you can't specify the IP or computer name? – Guy Starbuck Jan 26 '12 at 21:30
  • Did i get this right. You want a networt connection to some SQL Server without having to specify a hostname or ip address? – Mithrandir Jan 26 '12 at 21:31
  • 4
    I find it frightening that you are writing code to create a database without understanding how to even connect to a SQL Server, for what it's worth. – JNK Jan 26 '12 at 21:34
  • About the only way this can be done is if SQL Server is on the same machine this is running on, and is the defaul instance. In which case you say "(local)" or "localhost" as the server name (so you're not using the computer's dedicated name or IP). But if that's what you want, you'd have said. Right? – Chris J Jan 26 '12 at 21:34
  • 1
    Also, it looks like you're going to be open to SQL Injection attacks with an account that likely has elevated permissions. – EBarr Jan 26 '12 at 21:34
  • You can do it without the server name and IP if you switch to Oracle and use tnsnames instead. :) – tsells Jan 27 '12 at 04:54

1 Answers1

1

You could use SqlDataSourceEnumerator to get a list of all Sql Servers that are visible and browsable. This is not a good technique, since you could get an instance that you don't have the right to create a database on it, but you could still try something with that.

var enumerator = SqlDataSourceEnumerator.Instance;
foreach (DataRow row in enumerator.GetDataSources().Rows)
{
    var serverName = row["ServerName"];
    var instance = row["InstanceName"];

    // build a connection string and try to connect to it
}
Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
  • 1
    Sure - but in the end, you're also using a server name - you don't have to **know** it ahead of time, you can pick it from a list - but you're still using the server's name to connect to it – marc_s Jan 26 '12 at 21:43
  • I know, but the server name is not known to the user, so I though that could be what BShakiba is asking for. – Pierre-Alain Vigeant Jan 26 '12 at 21:46