1

I've made a code that create the database automatically in the folder where the software is executed. The DB was created succesfull, but when I create the object for the connection this exception is displayed:

System.Data.SqlClient.SqlException (0x80131904): There was a network error or specific instance while trying to establish 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): Can not find the file specified

on this line:

con.Open();

but the problem I think is on this declaration of the connection:

static SqlConnection con = new SqlConnection(
        "Server=localhost;Database=AppDB.sqlite;");

What am I doing wrong?

UPDATE Details:

 SqlCommand command = new SqlCommand(@"INSERT INTO Team (name,code, shortName, squadMarketValue, 
                crestUrl, link_self, link_fixtures, link_players, caption) VALUES (@name,
                @code,@shortName,@squadMarketValue,@crestUrl,@link_self,@link_fixtures,
                @link_players,@caption)", con);

How you can see I'm using parametizer query, and if I use this object:

static SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=SoccerForecast.sqlite;Version=3;");

the con variable is underlined in red, an the compilers tell me:

it is not possible to convert SqliteConnection in SqlConnection

Harold Finch
  • 576
  • 11
  • 32
  • The message is your answer. You are trying to connect to an SQL Server (microsoft). You need a SQLite connector. See here: http://stackoverflow.com/questions/3718362/how-can-i-access-sqlite-with-c – Jauch Jul 04 '15 at 13:59
  • I've updated the question, I want to establish a connection to my sqlite database – Harold Finch Jul 04 '15 at 14:23

1 Answers1

2

Your connection is wrong. You didn't specified the kind of database you're trying to connect to in your question. There's a mention of SQLite while your are using an sql connection object. The sql connection object is for connection to the ms sql server database. If you really want to connect to an ms sql server database, you'll need to change the connection string to provide a username / password or a Trusted_Connection=True to use your windows account.

However, if the database you are trying to connect to is an SQLite DB, you will have to use a different connection object. This and this SO question has answers about available libraries and examples that will explain how to create a connection to a SQLite database.

The site connectionstring.com is also a good resource when you need to generate a connection string.

Community
  • 1
  • 1
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
  • I want to establish a connection to the database locally on my pc, my db is in .sqlite extension – Harold Finch Jul 04 '15 at 14:16
  • Then you'll need to use an sqlite connector with an sqlite connection string. The SqlConnection is an ms sql native client won't work with other DB. – The_Black_Smurf Jul 04 '15 at 14:48
  • I've updated the question. I don't know how to use parameterized query, because if I execute this: command.Parameters.Add("@name",DbType.String).Value = item.name; I get a query fail insert. – Harold Finch Jul 04 '15 at 14:54
  • 1
    You also need to change your command object to the sqlite version – Jacob Lambert Jul 04 '15 at 15:09