-1

I am working on my first project using local database on C#. I have searched on internet different code for inserting data, but nothing has worked for me. I am trying different code, the problem that occurs to me is the built in functions they are using doesn't show up in my code. Can someone share the authentic code for inserting, retrieving and deleting in local database ?

The recent code that I have tried, some exception is occurring in SqlCeConnection.

This is my code :

string str="Data Source=(localdb)shop_database;Initial Catalog=shop_database;Integrated Security=True";

SqlCeConnection con = new SqlCeConnection(str);          
SqlCeDataAdapter sda = new SqlCeDataAdapter();
SqlCeCommand cmd = con.CreateCommand();

cmd.CommandText = "Insert into Account_details (Account_No,Customer_name,Customer_father_name,Profession,Mobile_No,Office_Address,House_Address,CNIC,Item_name,Item_color,Item_model,Item_engine_NO,Item_chasis_NO,Cash_price,Installment_price,Advance_given,Amount_left,Monthly_Installment,Monthly_Rent,Date_of_giving,Sponsor_name,Sponsor_father_name,Sponsor_profession,Sponsor_Address,Sponsor_CNIC,Sponsor_Mobile_No) values (@Account_No,@Customer_name,@Customer_father_name,@Profession,@Mobile_No,@Office_Address,@House_Address,@CNIC,@Item_name,@Item_color,@Item_model,@Item_engine_NO,@Item_chasis_NO,@Cash_price,@Installment_price,@Advance_given,@Amount_left,@Monthly_Installment,@Monthly_Rent,@Date_of_giving,@Sponsor_name,@Sponsor_father_name,@Sponsor_profession,@Sponsor_Address,@Sponsor_CNIC,@Sponsor_Mobile_No)";

cmd.Parameters.AddWithValue("@Account_No", this.Textbox0.Text);
cmd.Parameters.AddWithValue("@Customer_name", this.Textbox1.Text);
cmd.Parameters.AddWithValue("@Customer_father_name", this.Textbox2.Text);
cmd.Parameters.AddWithValue("@Profession", this.Textbox3.Text);
cmd.Parameters.AddWithValue("@Mobile_No", this.Textbox4.Text);
cmd.Parameters.AddWithValue("@Office_Address", this.Textbox5.Text);
cmd.Parameters.AddWithValue("@House_Address", this.Textbox6.Text);
cmd.Parameters.AddWithValue("@CNIC", this.Textbox7.Text);
cmd.Parameters.AddWithValue("@Item_name", this.Textbox14.Text);
cmd.Parameters.AddWithValue("@Item_color", this.Textbox15.Text);
cmd.Parameters.AddWithValue("@Item_model", this.Textbox16.Text);
cmd.Parameters.AddWithValue("@Item_engine_NO", this.Textbox17.Text);
cmd.Parameters.AddWithValue("@Item_chasis_NO", this.Textbox18.Text);
cmd.Parameters.AddWithValue("@Cash_price", this.Textbox19.Text);
cmd.Parameters.AddWithValue("@Installment_price", this.Textbox20.Text);
cmd.Parameters.AddWithValue("@Advance_given", this.Textbox21.Text);
cmd.Parameters.AddWithValue("@Amount_left", this.Textbox25.Text);
cmd.Parameters.AddWithValue("@Monthly_Installment", this.Textbox22.Text);
cmd.Parameters.AddWithValue("@Monthly_Rent", this.Textbox23.Text);
cmd.Parameters.AddWithValue("@Date_of_giving", this.Textbox24.Text);
cmd.Parameters.AddWithValue("@Sponsor_name", this.Textbox8.Text);
cmd.Parameters.AddWithValue("@Sponsor_father_name", this.Textbox9.Text);
cmd.Parameters.AddWithValue("@Sponsor_profession", this.Textbox10.Text);
cmd.Parameters.AddWithValue("@Sponsor_Address", this.Textbox11.Text);
cmd.Parameters.AddWithValue("@Sponsor_CNIC", this.Textbox12.Text);
cmd.Parameters.AddWithValue("@Sponsor_Mobile_No", this.Textbox13.Text);

try
{
    cmd.ExecuteNonQuery();
    MessageBox.Show("Successfully saved");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ahmer Mehmood
  • 23
  • 1
  • 8
  • 1
    For `localdb` you would use `SqlConnection`, `SqlCommand`, etc instead of Sql**Ce**... – Crowcoder Jan 20 '19 at 14:52
  • but every code on internet is using this method. – Ahmer Mehmood Jan 20 '19 at 14:59
  • Are you clear about what kind of database you want to use? if you want to use SQL Server, then you need to read about ADO.NET and learn how to use it to Save and retrieve data from SQL server. You code will not work for SQL Server for sure. – Chetan Jan 20 '19 at 15:04
  • 1
    There are many providers for ADO.Net. Sql, MySql, Sqlite, etc. You must use the correct provider for your database. If you are using localdb then SqlCe is not the correct provider. – Crowcoder Jan 20 '19 at 15:04
  • If you are using localdb (It's still not 100% clear) you will have an error in your connection string. You need backslashes: `...(localdb)\\shop_database...` – Crowcoder Jan 20 '19 at 15:07
  • I have tried your way now it is showing me following the source file is different – Ahmer Mehmood Jan 20 '19 at 15:10
  • 1
    It's also worth noting that this is a very poor database schema. Read up on database normalization and normal forms and try to design a better relational schema. – Daniel Mann Jan 20 '19 at 16:05
  • You're making too many mistakes, you need to get a book or tutorial. This stuff is simply too complicated to learn through trial and error. – Dour High Arch Jan 20 '19 at 16:57

2 Answers2

0

To edit, insert, in general interact with your database you need the class SqlCommand. First you create a connection to your database with an SqlConnection object. Then you pass the SQL statement as a string and the connection into the constructor of the SqlConnection class. Little example:

SqlConnection con = new SqlConnection("server=localhost;database=test_db;uid=root;password=yourpassword");
SqlCommand cmd = new SqlCommand("select * from your_table", con);

To retreive the data from the database you need to use the SQL Statements. For example an SQL statement is something like:

insert into my_table (value1, value2)
values("Example", "Insertion");

When you created your SqlConnection and the SqlCommand you need to open the database connection and execute the command. Wether it's a command for receiving information from the database or editing the database you use ExecuteReader() or ExecuteNonQuery(). For example when you want to receive all the Information stored in one table you use:

SqlConnection con = new SqlConnection("connection string as shown above");
SqlCommand cmd = new SqlCommand("select * from example_table", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
    Console.WriteLine(reader[<table_index or attribute Name>]);

And finally dont forget to call the close method on your SqlConnection and SqlDataReader object

0

You are probably making two mistakes:

Problem 1. Your connecting string looks like wrong. Instead of:

Data Source=(localdb)shop_database;Initial Catalog=shop_database;Integrated Security=True";

It should be:

Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=shop_database;Integrated Security=True";

Problem 2. You are not opening the connection before executing the command. Your code in the block should be like this:

try
{
    conn.Open(); // Open the connection
    cmd.ExecuteNonQuery();
    MessageBox.Show("Successfully saved");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    conn.Close(); // Close the connection
}

As a best practice, I recommend that you use "using" block to create your connection. In that case, you don't have to explicitly close the connection and set it to null:

try
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        conn.Open();
        // Remaining code
    }
}
catch(Exception ex)
{
    // Manage your exception here
}
Tanveer Yousuf
  • 386
  • 1
  • 3
  • 16
  • The localdb instance name does not need to be "MSSQLLocalDB" but it is suspect that it is named the same as the database. – Crowcoder Jan 20 '19 at 15:54
  • Yes it doesn't. However, it's likely to be MSSQLLocalDB if it is indeed SQL Server LocalDb as it's inferred by the connection string instead of normal edition of SQL Server. – Tanveer Yousuf Jan 20 '19 at 16:08