Using a database in .NET.

UPDATED QUESTION:

Well, I'm going to simplify my question, since I do not know how to answer your questions.

Let's say I create a new Windows Forms application, then Project-> Add New Item-> Local Database.

Then, in Database Explorer, I create a table ("testtable") and give it the "ID" column and the "VALUE" column.

Can you provide me the steps to just add a row to the database from the code?

OLD QUESTION:

I tried to do something, which I think would be very easy, but I had never used C # before and there were problems with the details. I just want to use sql database with Visual C # Express 2008.

For testing purposes, I have a datagrid in my form that can reflect changes in db.

If I use this:

codesTableAdapter.Fill(dataSet1.codes);

( ) sql.

- :

codesTableAdapter.InsertQuery(txtCode.Text,txtName.Text);
codesTableAdapter.Fill(dataSet1.codes);
codesTableAdapter.Update(dataSet1);
dataSet1.AcceptChanges();

, . , .

, , , , .

+2
5

, , , :

, Windows Forms, Project- > Add New Item- > Local Database

SQL Server Compact Edition.SDF . Visual Studio SQL CE Query Analyzer.

Database Explorer ( "testtable" ) "ID" "VALUE".

.SDF. , .

, ?

System.Data.SqlServerCe .SDF. :

using (SqlCeConnection myConnection = new SqlCeConnection(@"Data Source=Path\To\Your\SDF\file.sdf;"))
using (SqlCeCommand myCmd = myConnection.CreateCommand())
{
    myCmd.CommandType = CommandType.Text;
    myCmd.CommandText = "INSERT INTO testtable (ID, [VALUE]) VALUES (1, whatever)";
    myConnection.Open();
    myCmd.ExecuteNonQuery();
}

, Path\To\Your\SDF\file.sdf , ; Visual Studio (, \to\\\bin\Debug\file.sdf). , , .SDF , , . , , , .

, , , " " VS ", ".

+2

DataRow DataSet ?

0

, , . SqlCE? , , , , ...

0

, - .

DataTable dt = dataSet1.Tables["codes"];
codesTableAdapter.InsertQuery(txtCode.Text,txtName.Text); 
codesTableAdapter.Fill(dt); 
// Add a new row to your datatable;
DataRow dr = dt.NewRow();
dr["ID"] = 100;
dr["Value"] = "This is my value";  // this is assumeing the 'Value' columns is of type string.
dt.Rows.Add(dr);

codesTableAdapter.Update(dataSet1); 
dataSet1.AcceptChanges(); 
0

All Articles