使用.NETCF以编程方式添加和更新记录

时间:2022-07-17 03:46:11

OK - I have worded this search 40 different ways and I seem to be lost here.

好的 - 我已经用40种不同的方式措辞了这个搜索,我似乎迷失在这里。

Every example I find seems so happy that you can easily drag and drop a datagrid and let the user fill it in -- then they stop! I know how to do everything I am asking through LINQ. That obviously won't translate here. I really should have learned ADO.NET first, then LINQ, but NOoooo...

我发现的每个例子都很高兴您可以轻松地拖放数据网格并让用户填写它 - 然后它们就会停止!我知道如何通过LINQ做我要问的一切。这显然不会在这里翻译。我真的应该先学习ADO.NET,然后是LINQ,但是NOoooo ......

I need to know how to do the following in .NETCF (Windows Mobile 5) using a SQL CE database on the device.

我需要知道如何在.NETCF(Windows Mobile 5)中使用设备上的SQL CE数据库执行以下操作。

  1. Add a new record and populate some or all of the fields with data I supply. I don't need to add a record to a datagrid - sometimes the user will not even see the record. How do I add a new record -- put data into it and save it? For example: Create a new delivery record, say, and have the program store the date in one field and a number in another field.

    添加新记录并使用我提供的数据填充部分或全部字段。我不需要向数据网格添加记录 - 有时用户甚至不会看到记录。如何添加新记录 - 将数据放入其中并保存?例如:创建一个新的交付记录,并让程序将日期存储在一个字段中,将数字存储在另一个字段中。

  2. Search for a record, then update data in it. Again, using LINQ I can do this easily -- I cannot for the life of me find any examples of doing it without it. I can find lots of examples of populating a grid of databound fields, letting the user make changes then saving it out. I don't need to do that.

    搜索记录,然后更新其中的数据。再次,使用LINQ我可以轻松地做到这一点 - 我不能在我的生活中找到任何没有它的例子。我可以找到许多填充数据绑定字段网格的示例,让用户进行更改然后将其保存。我不需要这样做。

Say I need to search for the one record that meets a criteria (customerID=10 and orderID=1234), then when (if) that record is found, update a field in it.

假设我需要搜索符合条件的一条记录(customerID = 10和orderID = 1234),然后在找到该记录时(if)更新其中的字段。

Please let me know if you need any more info. I have done a lot of reading and just seem to be missing something!

如果您需要更多信息,请告诉我。我做了很多阅读,似乎错过了一些东西!

Thanks in advance...

提前致谢...

3 个解决方案

#1


For #1, there are a few ways. Two easy, common ways are:

对于#1,有几种方法。两种简单,常见的方式是:

Use a SqlCeResultset, and create a row with it, then insert.

使用SqlCeResultset,并使用它创建一行,然后插入。

using(SqlCeConnection connection = new SqlCeConnection(myConnString))
using (SqlCeCommand cmd = new SqlCeCommand())
{
    connection.Open();
    cmd.CommandText = "MyTable";
    cmd.CommandType = CommandType.TableDirect;
    cmd.Connection = Connection;
    SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable);

    SqlCeUpdatableRecord record = rs.CreateRecord();

    // do something to set your values in the record

    rs.Insert(record);
}

Use a SqlCeCommand, set the command SQL and call ExecuteNonQuery.

使用SqlCeCommand,设置命令SQL并调用ExecuteNonQuery。

using (SqlCeConnection connection = new SqlCeConnection(myConnString))
using (SqlCeCommand cmd = new SqlCeCommand())
{
    connection.Open();
    cmd.CommandText = "INSERT INTO MyTable(Foo) VALUES('bar')";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = Connection;
    cmd.ExecuteNonQuery();
}

For #2 it's not much different. Use an updateable resultset, seek to the record, modify and call Update or build your SQL and send it through the command using ExecuteScalar or ExecutNonQuery. The code is similar enough to those above that I'll leave it to you to work out.

对于#2来说,它并没有太大的不同。使用可更新的结果集,查找记录,修改并调用Update或构建SQL,并使用ExecuteScalar或ExecutNonQuery通过命令发送它。代码与上面的代码相似,我会留给你解决。

A quick search on "SqlCeResultSet" and "example" or "update example" give loads of relevant results.

快速搜索“SqlCeResultSet”和“示例”或“更新示例”可以提供大量相关结果。

#2


Also for anybody else searching this book also has a really good chapter on this procudere: http://my.safaribooksonline.com/0321174046

此外,对于搜索本书的其他人来说,这本书也有一个非常好的章节:http://my.safaribooksonline.com/0321174046

I am still working my through this issue but making good progress,

我仍在努力解决这个问题,但取得了良好的进展,

Thanks for the help.

谢谢您的帮助。

#3


Thank you,

Between that and this code here, I think I am on my way.

在此代码与此代码之间,我想我正在路上。

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

    Dim con As SqlCeConnection

    con = New SqlCeConnection(("Data Source = " & Me.NorthwindDatabaseFullPath))
    con.Open()

    Dim sqlcdm As String = "INSERT INTO Orders VALUES(999, 'ALFKI', 1, 'RUDEDOG', '', '', '', '', '', 2, '11/15/1967', '11/16/1967', '11/17/1967', '23')"

    Dim SqlCeCommand As New SqlCeCommand(sqlcdm, con)

    SqlCeCommand.ExecuteNonQuery()

End Sub

#1


For #1, there are a few ways. Two easy, common ways are:

对于#1,有几种方法。两种简单,常见的方式是:

Use a SqlCeResultset, and create a row with it, then insert.

使用SqlCeResultset,并使用它创建一行,然后插入。

using(SqlCeConnection connection = new SqlCeConnection(myConnString))
using (SqlCeCommand cmd = new SqlCeCommand())
{
    connection.Open();
    cmd.CommandText = "MyTable";
    cmd.CommandType = CommandType.TableDirect;
    cmd.Connection = Connection;
    SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable);

    SqlCeUpdatableRecord record = rs.CreateRecord();

    // do something to set your values in the record

    rs.Insert(record);
}

Use a SqlCeCommand, set the command SQL and call ExecuteNonQuery.

使用SqlCeCommand,设置命令SQL并调用ExecuteNonQuery。

using (SqlCeConnection connection = new SqlCeConnection(myConnString))
using (SqlCeCommand cmd = new SqlCeCommand())
{
    connection.Open();
    cmd.CommandText = "INSERT INTO MyTable(Foo) VALUES('bar')";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = Connection;
    cmd.ExecuteNonQuery();
}

For #2 it's not much different. Use an updateable resultset, seek to the record, modify and call Update or build your SQL and send it through the command using ExecuteScalar or ExecutNonQuery. The code is similar enough to those above that I'll leave it to you to work out.

对于#2来说,它并没有太大的不同。使用可更新的结果集,查找记录,修改并调用Update或构建SQL,并使用ExecuteScalar或ExecutNonQuery通过命令发送它。代码与上面的代码相似,我会留给你解决。

A quick search on "SqlCeResultSet" and "example" or "update example" give loads of relevant results.

快速搜索“SqlCeResultSet”和“示例”或“更新示例”可以提供大量相关结果。

#2


Also for anybody else searching this book also has a really good chapter on this procudere: http://my.safaribooksonline.com/0321174046

此外,对于搜索本书的其他人来说,这本书也有一个非常好的章节:http://my.safaribooksonline.com/0321174046

I am still working my through this issue but making good progress,

我仍在努力解决这个问题,但取得了良好的进展,

Thanks for the help.

谢谢您的帮助。

#3


Thank you,

Between that and this code here, I think I am on my way.

在此代码与此代码之间,我想我正在路上。

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

    Dim con As SqlCeConnection

    con = New SqlCeConnection(("Data Source = " & Me.NorthwindDatabaseFullPath))
    con.Open()

    Dim sqlcdm As String = "INSERT INTO Orders VALUES(999, 'ALFKI', 1, 'RUDEDOG', '', '', '', '', '', 2, '11/15/1967', '11/16/1967', '11/17/1967', '23')"

    Dim SqlCeCommand As New SqlCeCommand(sqlcdm, con)

    SqlCeCommand.ExecuteNonQuery()

End Sub