The specified table does not exist. [ table_Name] error - c#

Hi all I have an small application to add the expense of the day.
For this I am using SQL compact database (CE). While inserting the record into a table name Expenses I am getting error
The specified table does not exist. [Expenses]
Insertion code is
using (var con =new SqlCeConnection(#"Data Source=|DataDirectory|\Database\Acadamy.sdf;
Persist Security Info=False"))
{
con.Open();
try
{
var Cmd = new SqlCeCommand();
String sqlAddNew = #"INSERT INTO Expenses (name, amount,receipt,details)
Values(#name,#amount,#receipt,#details)";
Cmd = new SqlCeCommand(sqlAddNew, con);
Cmd.Parameters.Add("#name", SqlDbType.NVarChar).Value = txtName.Text;
Cmd.Parameters.Add("#amount", SqlDbType.NVarChar).Value = txtAmount.Text;
Cmd.Parameters.AddWithValue("#receipt", SqlDbType.NVarChar).Value = txtRecept.Text;
Cmd.Parameters.AddWithValue("#details", SqlDbType.NVarChar).Value = txtDetails.Text;
Cmd.ExecuteNonQuery();
}
catch (Exception exception)
{
txtAmount.Text = exception.ToString();
}
finally
{
if (con.State == ConnectionState.Open) con.Close();
}
}
}
I am not getting why this error occurring.
Acadamy.sdf structure is as below:
I am able to retrieve data from another table of the same database. What will be the problem?

Whenever I get an error about a table that does not exist I use the SQL Server Management Studio and check if the table really is missing or if I just have a typo in my query.
Unfortunately the current version of the Management Studio no longer supports SQL Server Compact Database and you will have to use the 2008 version. You can get it directly from Microsoft: SQL Server 2008 R2 Management Studio Express

Related

Insert data to Visual studio local DB, using C#

I am making an application, and I added a local database in visual studio. It all works fine while logging into app using crediantials stored in the database. But while adding new records, the query runs with no error but data is not actually stored in data base. Every thing else is working fine, except for the data not being stored.
Here is my code:
SqlConnection con = new SqlConnection(#" Data Source = (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security = True");
SqlCommand cmd = new SqlCommand("insert into users(username,password,phone,adress,father_name,email)VALUES('"+username.Text+ "','"+password.Text+ "','"+phone.Text+ "','"+adress.Text+ "','"+fathername.Text+ "','"+email.Text+"')",con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show(username.Text + " Has been Added");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}

sqlite insert with c#

i am testing my sqlite local server with c#, I have the connection and query setup without problem. I tried to copy the query to sqlite and it runs without problem. However, when I run it in my program, nothing insert into the db. Wondering what the problem is.
I have set the db build action to Content, and the copy to output directory options to copy if newer
private void insertIntoDB(string query)
{
using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=.\\VHTDatabase.db"))
{
using (System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(conn))
{
conn.Open();
Console.WriteLine(query);
cmd.CommandText = query;
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
Try a full path to your database in your Connectionstring
Then maybe try to add the CommandText before you open the Connection.
i mean:
cmd.CommandText = query;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Does your database give you informations with SELECT?
Maybe you need to reconfigure the User of the Database and give him rights to write, change and delete.
If nothing helps, then you can check, if it gives you an error.
try
{
cmd.CommandText = query;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}

using SQLite database for smart device VS 2008

I'm developing an application for my WindowsCE packect PC(.netCF35) to read some parameters and record them in my SQLite database in Visual Studio 2008. I knew that I need to add SQLite references to my application (I had a succeed experience for my windows 7 application). So I downloaded related files from http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki and I just could add System.Data.SQLite.dll (I couldn't add SQLite.Interop.102.dll to my references). Now when I run my application, I can't add any data to my database and it seems cmdwrite.ExecuteNonQuery(); doesn't work correct. I need to say my table name and structure is correct(I'm sure). You can see my code as follow:
SQLiteConnection con = new SQLiteConnection("Data Source=AMI.sqlite;Version=3;");
public bool Insert(string Meter_ID, string type, string readout, string timestamp)
{
sqlwrite = "INSERT INTO meters (id,type,val,timestamp) VALUES(?,?,?,?)";
try
{
SQLiteCommand cmdwrite = new SQLiteCommand(sqlwrite, con);
cmdwrite.Parameters.AddWithValue("#id", Meter_ID);
cmdwrite.Parameters.AddWithValue("#type", type);
cmdwrite.Parameters.AddWithValue("#val", readout);
cmdwrite.Parameters.AddWithValue("#timestamp", timestamp);
con.Open();
dbstate = "3";
cmdwrite.ExecuteNonQuery();
dbstate = "4";
con.Close();
return true;
}
catch
{
con.Close();
return false;
}
}
Insert("123", "456", "789", "123");
Does anybody have any experience to work with SQLite database in WindowsCE?

Issue with connectionString. Using Visual studio 2013 pro. trying to connect to employee DB on SQL Server 2012,

I am trying to connect to SQL Server 2012 express. There is a database called employee which I would like to save data from a WPF form to a table called [dbo].[EVUSERS]. It is stored in my local Database. From some examples I see that "Data Source=.\SQLEXPRESS" Is this correct? Or should I specify the table aswell? using localhost as the server doesn't work either.
I get an error saying "the server was not found or was not accessible."
Do I have to configure the server in some way to receive connections?
Here is my attempt.
void saveData()
{
try
{
var firstName = fNameTextbox.Text;
var lastName = LNameTextBox.Text;
var userName = UserName.Text;
String pass = PasswordTextBox.Password;
String confirm = ConfirmTextBox.Password;
int loggedIn = 1;
//parameterise values
string connectionString = #"Data Source=.\SQLEXPRESS;Database=Employee";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO [dbo].[EVUSERS] (UName, Pass, FName, LName, Attempts, LastLogin, LoggedIn) VALUES (#UName, #Pass, #FName, #LName, #Attempts, #LastLogin, #LoggedIn)";
command.Parameters.AddWithValue("#UName", UserName);
command.Parameters.AddWithValue("#Pass", pass);
command.Parameters.AddWithValue("#FName", firstName);
command.Parameters.AddWithValue("#LName", lastName);
command.Parameters.AddWithValue("#Attempts", attempts);
command.Parameters.AddWithValue("#LastLogin", lastLogIn);
command.Parameters.AddWithValue("#LoggedIn", loggedIn);
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("command number of rows = " + command);
}
//connection.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
Here is a screenshot of the server connection.
Many Thanks
.\SQLExpress is correct. If the database name is correct (although for SQL Express it should be a path to the file, yes?) then you can add ";Integrated Security=SSPI" to the connection string and you'll be OK if you are the user who installed it.
Can Use It For Connect With Sql Server 2012 :
var connectionString = "Server=127.0.0.1;DataBase=Employee;
User Id=(sa or your user id without Parenthesis);
Password=(your password without Parenthesis);";

C# database writing to table doesn't work

I have a problem using Visual Studio 2010. I am using a service-based database (.mdf).
I have created a table manually in Visual Studio with some information. The code I have written can read from the table but when I insert new information it looks like the data is added to some other database.
I can read information from the correct table but when I add information to the table I can’t see the changes in Server Explorer in Visual Studio.
I don't know why to different databases are used! Does anybody know the problem?
Here is my code:
public class DataAccess
{
private SqlConnection sqlConnection;
private SqlCommand sqlCommand;
private SqlDataAdapter dataAdapterAnimal;
private DataSet dataset;
private string connectionString = DBAccessLayer.Properties.Settings.Default.AnimalDBConnectionString;
public DataSet LoadAnimalDataSet()
{
dataset = new DataSet();
using (sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
dataAdapterAnimal = new SqlDataAdapter("SELECT * FROM AnimalTable", sqlConnection);
dataAdapterAnimal.Fill(dataset, "AnimalTable");
sqlConnection.Close();
return dataset;
}
}
public void AddAnimal(int animalID, string name, double age, string category, string gender, string extraAnimalInfo)
{
sqlConnection = new SqlConnection(connectionString);
sqlCommand = new SqlCommand("INSERT INTO AnimalTable VALUES(#AnimalID, #Name, #Age, #Category, #Gender, #ExtraAnimalInfo)", sqlConnection);
try
{
sqlConnection.Open();
sqlCommand.Parameters.Add(new SqlParameter("#AnimalID", animalID));
sqlCommand.Parameters.Add(new SqlParameter("#Name", name));
sqlCommand.Parameters.Add(new SqlParameter("#Age", age));
sqlCommand.Parameters.Add(new SqlParameter("#Category", category));
sqlCommand.Parameters.Add(new SqlParameter("#Gender", gender));
sqlCommand.Parameters.Add(new SqlParameter("#ExtraAnimalInfo", extraAnimalInfo));
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
catch (Exception ex)
{
throw;
}
}
I haven't seen your connection string yet - but from your description, it seems it might be this problem here:
the whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!
If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.
The real solution in my opinion would be to
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g. MyDatabase)
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=.\\SQLEXPRESS;Database=MyDatabase;Integrated Security=True
and everything else is exactly the same as before...

Categories