Birger Sørensen

Birger Sørensen

  • NA
  • 37
  • 3.1k

LocalDB in Visual Studio 2015 - SQL manipulation of database

Mar 17 2017 1:54 PM
New to Visual Studio, and to some extend to C#.
But not to programming OOP or SQL.

I need a databse for local use only, nothing advanced, and was suggested loacalDB.
Have tried a couple of others, that are now removed, so only loacalDB exists.

I can connect to a database File (which is what localDB apperently does?), not an actual server.
Works like a charm, and I can set up my tables.
So I tried to create a simple application and database, that contains 1 table with 3 attributes : id, name and rel (for relation).
Fine.
Insert a datagrid on the form - can edit rows - and they are not saved.
Adding a TableAdapter and manually executing SQL command I get a Sqlexception : object "myfamily" does not exist.
myfamily is the name of my datatable, and the only place it appears is in the query as the FROM attribute....

Trying to figure out, what is wrong, the only thing that can be wrong is the connection string.
TableAdapter shold be able to figure this one out on its own, and has no problem connectiong to the database - but can not find the table.
Manually (as per https://msdn.microsoft.com/en-us/library/fksx3b4f.aspx at the bottom) I also get the Sqleception object "myfamily" does not exist
private int IsInData(String navn, SqlConnection conn)

{
int idx = -1;
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT id FROM myfamily WHERE navn='" + navn +"'";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
reader = cmd.ExecuteReader();   <= Error occurs here
idx = (reader.HasRows) ? (int)reader.GetValue(0) : -1; // Data is accessible through the DataReader object here.
return idx;
}



I tried to set the path in connectionstring to te original and the copied version - and it really doesn't make any difference...
 
So I can do anything I want except use my table for anything...
Any suggestions? Please?

Answers (2)