2013-04-10 134 views
1

我从别人继承了这一项目,并需要清理,以满足新的要求,另一个表中提取数据 - 他们已经设计了它,像这样..INSERT INTO表由

我在访问2个表数据库(我已经包括在大括号中的字段的数据类型):

活动:

事件ID {自动编号} | EventTitle {短文本} | EventDescription {长文本} | EventDate {Date/Time} | EventCategory {查找与类别的排源/ CategoryTitle}

分类:

类别ID {自动编号} | CategoryTitle {短文本} | CategoryImage {短文本} | CategoryColor {Short Text}

'Categories'表包含关于特定类别的所有'Master'信息,例如名称,图标(例如刻度线,十字等)。

而且我有一个查询(访问内部)这两个表中的数据拉在一起,下面的字段顺序:

事件ID | EventTitle | EventDescription | EventDate | CategoryTitle | CategoryImage

该查询的SQL是:

SELECT Events.EventID, Events.EventTitle, Events.EventDescription, 
Events.EventDate, Categories.CategoryTitle, Categories.CategoryImage, 
Categories.CategoryColor FROM Events INNER JOIN Categories ON 
Events.EventCategory = Categories.CategoryID ORDER BY 
Events.EventDate; 

网站的其他领域依赖查询检索信息。

在Visual Studio中,我试图从插入文本框的ASP值和下拉控件到从我的数据库下面的字段:

  1. EVENTTITLE(以Events.EventTitle)
  2. EventDescription(以Events.EventDescription )
  3. EVENTDATE(至Events.EventDate)
  4. EventCategory(至Events.EventCategory)

编号4是有问题的,因为它目前是对“主”事件表的访问查找,该表包含有关实际类别的信息。据推测,从未打算将数据以这种方式插入表中;设置事件类别的唯一可能方式是使用Access数据库内的下拉组合框来选择事件。

最后这里是子程序我写插入到数据库:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString) 
    conn.Open() 
    Dim cmd As New OleDbCommand("INSERT INTO Events (EventTitle, EventDescription, EventDate, EventCategory) VALUES (@f1,@f2,@f3,@f4)", 
    conn) 
    cmd.Parameters.AddWithValue("@f1", tb_eventtitle.Text) 
    cmd.Parameters.AddWithValue("@f2", tb_eventdescription.Text) 
    cmd.Parameters.AddWithValue("@f3", DateTime.ParseExact(tb_eventdate.Text, "dd/MM/yyyy", 
    CultureInfo.InvariantCulture)) 
    cmd.Parameters.AddWithValue("@f4", dd_eventcategory.SelectedValue) 
    cmd.ExecuteNonQuery() 
    conn.Close() 
End Sub 

还有其他ASP.NET databinded上依赖于的“类别”的内容,网站的其他控制表格保持不变,所以我怎么能插入'事件'表与我上面列出的字段(特别是 EventCategory)?

编辑:

这里是我完成的子:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles 
    Button1.Click 
    Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString) 
    conn.Open() 
    Dim cmd As New OleDbCommand("INSERT INTO Events (EventTitle, EventDescription, EventDate, EventCategory) VALUES (@f1,@f2,@f3,@f4)", 
    conn) 
    cmd.Parameters.AddWithValue("@f1", tb_eventtitle.Text) 
    cmd.Parameters.AddWithValue("@f2", tb_eventdescription.Text) 
    cmd.Parameters.AddWithValue("@f3", DateTime.ParseExact(tb_eventdate.Text, "dd/MM/yyyy", 
    CultureInfo.InvariantCulture)) 
    cmd.Parameters.AddWithValue("@f4", dd_eventcategory.SelectedIndex + 1) 
    cmd.ExecuteNonQuery() 
    conn.Close() 
End Sub 
+0

'EventCategory'查找字段是否允许多个选择? – 2013-04-10 19:18:09

+0

@Gord Thompson:不,只有单一选择,即“公开”,“私人”或“仅限群组” – adaam 2013-04-10 19:20:35

+0

@dasblinkenlight:对不起,这是一个错误,我会重新发布我的工作代码(我只是玩弄选择多个表,但意识到我的问题的解决方案将需要比简单地执行选择查询更多的工作) – adaam 2013-04-10 19:24:40

回答

2

查阅字段可以欺骗。假设你有你的[类别表

CategoryID CategoryTitle 
1   Pub Crawl 
2   Booze Up 
3   Big Drunk 

和你的[活动]表看起来是这样的,当你在数据表视图

EventID EventTitle EventCategory 
1  Thursday Pub Crawl 

打开它,如果你想添加一个新的“星期五”您事件倒是认为,这将是像

INSERT INTO Events (EventTitle, EventCategory) VALUES ("Friday", "Big Drunk") 

但失败‘的Microsoft Access设置1个字段为Null由于类型转换失败...’。取而代之的是,你必须做

INSERT INTO Events (EventTitle, EventCategory) VALUES ("Friday", 3) 

其中3数字类别ID,而不是你在数据表视图看到。

因此,您需要做的是确保您从组合框(不是CategoryTitle)中检索(数字)CategoryID并将其插入到Events表中。

顺便说一句,这就是为什么更丰富的接入开发商避免查找字段:他们隐藏了数据库的实际运作和导致混乱,沮丧,是的,迫使一些用户喝....;)

+0

哈哈非常感谢你!我无法理解查询领域的事情,尤其是在将它与VB中的SQL查询结合使用时。我有它的工作。 (我已经在上面发布了工作代码) – adaam 2013-04-10 19:36:21