0

I have added a SQL Server .mdf database file to my C# application, but when I try to connect with this code, the program causes a connection error.

CODE:

DataSet data;

string con = "Data Source=dbinterno.mdf;";
string queryString = "Select * FROM Dati";

try
{
    using (SqlConnection connection = new SqlConnection(con))
    {
        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter();

        SqlCommand command = new SqlCommand(queryString, connection);

        command.ExecuteNonQuery();

        data = new DataSet();
        adapter.Fill(data);
        MessageBox.Show(data.ToString());

        connection.Close();
    }
}
catch
{
    MessageBox.Show("\n Problemi di connessione al database");
}

The error is:

ERROR IMAGE

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rikideveloper
  • 21
  • 1
  • 6
  • 3
    We need more information... What's the error for starters? – trousyt Mar 14 '16 at 19:20
  • There is no clear information. We don't know what is the problem. – onatm Mar 14 '16 at 19:22
  • @trousyt i have insert error image – rikideveloper Mar 14 '16 at 19:24
  • @onatm i have insert error image – rikideveloper Mar 14 '16 at 19:24
  • ExecuteNonQuery is for Updates, Inserts and Deletes, not SELECT. Your adapter isn't doing anything. – LarsTech Mar 14 '16 at 19:25
  • 1
    did you try to use an absolute mdf file path instead of a relative one? – vc 74 Mar 14 '16 at 19:25
  • 1
    if this `string con = "Data Source=dbinterno.mdf;";` is all of your connection string then it's not completed. connection string should be, for example, `Server=.\SQLSERVER;Initial Catalog=dbinterno;Integrated Security=True` and so on... search google for connection string in mssql server... like error said `the server not found...` because it's not defined in connection string. – nelek Mar 14 '16 at 19:26
  • Did you look at [DB Connection string in Web.config to use attached .mdf database won't work](http://stackoverflow.com/questions/7687930/db-connection-string-in-web-config-to-use-attached-mdf-database-wont-work) or perhaps a [google search](https://www.google.com/search?q=mdf+connection+string&ie=utf-8&oe=utf-8)? – crashmstr Mar 14 '16 at 19:27
  • btw. if you use `SqlDataAdapter` then you don't need `SqlCommand` at all. Instead `SqlCommand` (which you don't need at all), put your queryString and connection string in `SqlDataAdapter` for example : `string con = "Server=.\SQLSERVER;Initial Catalog=dbinterno;Integrated Security=True"; string queryString = "SELECT * FROM Dati;"; SqlDataAdapter adapter = new SqlDataAdapter(queryString, con);` and then populate `DataSet`. – nelek Mar 14 '16 at 19:33

1 Answers1

-1

Here are a couple observations:

  1. Your connection string will need to be modified. Try using string con = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"; using Windows Authentication or this:
    string con = "Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;"; using standard security, Source: connectionstrings.com. This should be managed some other way than in code as well. Desktop applications can be de-compiled, and if the password changes, you would need a rebuild. In a ASP.NET application, Microsoft advises to use a web.config file or in the windows registry using a custom subkey.

  2. You will want to use ExecuteReader() for a SELECT statement as ExecuteNonQuery() will not return a result set. See this answer that describes the differences in the types of SQL Server methods

  3. you don't need connection.Close();, the using statement will handle that.
Community
  • 1
  • 1
AndrewK
  • 1,223
  • 2
  • 16
  • 25
  • Your answers under 1. it's better to show user some examples then link to some web site (which "tomorrow" can be "closed"... and, at least, you have to make a effort pointing user to specific link, since "we" talking about mssql server) and it's not *probably* but *have to* and have to very much; under 2. not *probably* but *must* or *have to*, again... – nelek Mar 14 '16 at 19:47