Jump to content

asp.net + ms sql

- - - - -

  • Please log in to reply
11 replies to this topic

#1
nikodz

nikodz

    Newbie

  • Members
  • PipPip
  • 12 posts
hello.
I want to connect MS SQL with asp.net framework 2.0

using Windows 7, IIS 7.0, Visual Studio 2010. Also using programming language C#.

Thanks.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
There appears to be a nice tutorial here:
Connecting to MSSQL Server in ASP.NET

I am sure there is great documentation on Microsoft's MSDN network.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
nikodz

nikodz

    Newbie

  • Members
  • PipPip
  • 12 posts
Alexandre
Thanks.

Here I learn how to connect MS SQL Server, do you know how to connect mdf (*.mdf) file?
I think I have problem with connection string.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
What does yours look like? It may be something like this that should work:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
There are some more connection string examples here:
SQL Server 2008 Connection String Samples - ConnectionStrings.com
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
nikodz

nikodz

    Newbie

  • Members
  • PipPip
  • 12 posts
I want to connect MS SQL.
Line 17:         SqlConnection con = new SqlConnection("Server=.\\SQLExpress;AttachDbFilename=|DataDirectory|Database.mdf;Database=Users; Trusted_Connection=Yes;");

Line 18:         con.Open();

Line 19:         con.Close();
Error on line 18

Also, cant connect SQL Server 2005 :|

#6
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Hmm could you copy the exception message and paste it in here, wrap the above code in try catch in catch block publish the message so that you can copy it

#7
nikodz

nikodz

    Newbie

  • Members
  • PipPip
  • 12 posts

Quote

private void asd(){
SqlConnection con = new SqlConnection("Data Source = Niko-PC\\Niko;Initial Catalog = master; Integrated Security = true;");
try{
con.Open();
con.Close();
}
catch(Exception e){
loutput.Text = e.ToString();
}
}

Quote

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 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 _Default.asd() in d:\IIS\Start\www\Default.aspx.cs:line 28

......

#8
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
ok just a few questions,
What is the instance name of SqlExpress that you are running,
Is it Niko? If yes i also want you to do this,
Open the Service Manager Start + Run = services.msc and hit enter
Go to Sql server browser and start the service. Set it to automatic.
Now try again

#9
nikodz

nikodz

    Newbie

  • Members
  • PipPip
  • 12 posts
gokuajmes
Thanks! instance name is SqlExpress ... That was reason.

Thanks again.

#10
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
You are welcome have a happy day

#11
nikodz

nikodz

    Newbie

  • Members
  • PipPip
  • 12 posts
I connected to Sql Server, but now I need to allow new Database (which I made).

#12
nikodz

nikodz

    Newbie

  • Members
  • PipPip
  • 12 posts
I did it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users