0

I am writing a C# application that interfaces with SQL Server CE, but I have encountered one of the many limitations that CE has.

My database has the tables Customer, List, and CustomerList. Customers can be subscribed to many lists and lists can have many customers - so I have the customerlist table as a junction table (columns are CustomerListId, CustomerId, ListId - that's all).

The design is that we have a master list that will always have every customer listed in it.

So in my code I am writing a function that sets all the listed customers to be subscribed to the master list. This is my very simple SQL query:

insert into customerlist (CustomerId, ListId)
values (
(select customerid from customer),
(select 1 from list where ShortDesc='Master'))

This query does not work in SQL Server CE and I am racking my brains trying to find an alternative that does not involve a painful for-each row-by-row code. Any ideas would be greatly appreciated.

4

2 回答 2

1

为什么不直接使用:

INSERT INTO  customerlist (CustomerId, ListId) 
SELECT customerid,1  FROM customer

我错过了什么吗?

祝你好运。

于 2013-02-05T13:38:54.133 回答
0

我想出了一个解决方案。

我首先做了一个获取主列表 ID 的 C# cmd。

string sqlGetId = "select listid from list where ShortDesc='Master'";
SqlCeCommand cmdGetMasterId = new SqlCeCommand(sqlGetId, DbConnection.ceConnection);
int key = (int)cmdGetMasterId.ExecuteScalar();

然后我在更简单的第二个查询中使用了该键:

sql2 = "insert into customerlist (CustomerId, ListId) select customerid, " + key + " from customer";
cmd2 = new SqlCeCommand(sql2, DbConnection.ceConnection);
cmd2.ExecuteNonQuery();

我意识到添加带参数的 SQL 命令总是比连接更好 - 但由于此应用程序的范围,SQL 注入不是问题。

(很抱歉这个简单的问题/答案,但我是新手^^)

于 2013-02-05T14:20:49.837 回答