I am trying to use C# to create a SQL Server database. I have not yet figured out what the connectionstring should be. Have failed using a variety of ideas.
Here is the code I'm trying:
I have tried providing "localserver" in place of "Compaq330" and "master" in place of "SQLEXPRESS.master".
The database does not get created.
The line in bold throws this exception:
This connectionstring works fine for an already existing database:
Any assistance would be appreciated.
Here is the code I'm trying:
Code:
System.Data.SqlClient.SqlConnection tmpConn;
tmpConn = new SqlConnection();
tmpConn.ConnectionString
= "SERVER = COMPAQ330; "
+ "DATABASE = SQLEXPRESS.master; "
;
string sqlCreateDBQuery;
sqlCreateDBQuery = " CREATE DATABASE"
+ " PeggyTest"
;
SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);
try
{
[B]tmpConn.Open();[/B]
MessageBox.Show(sqlCreateDBQuery);
myCommand.ExecuteNonQuery();
MessageBox.Show("Database has been created successfully!",
"Create Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "Create Database",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
finally
{
tmpConn.Close();
}
I have tried providing "localserver" in place of "Compaq330" and "master" in place of "SQLEXPRESS.master".
The database does not get created.
The line in bold throws this exception:
Code:
System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at File_Browser.Class1.myDatabase() in C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\File Browser\File Browser\Class1.cs:line 35
This connectionstring works fine for an already existing database:
Code:
SqlConnection conn = new SqlConnection(
"Data Source=.\\SQLEXPRESS;"
+ "AttachDbFilename=C:\\Northwind\\NORTHWND.MDF;"
+ "Integrated Security=True;"
+ "Connect Timeout=30;"
+ "User Instance=True");