C# Access数据库查询字符串

C# Access数据库查询字符串,c#,asp.net,sql,database,C#,Asp.net,Sql,Database,我有一个包含两个表的数据库;StudentID是“StudentList”表上的主键,StudentID是“JournalEntries”表上的外键 在我的网站上,我有一个搜索功能,用户可以从学生列表中搜索学生,结果显示在数据网格中。当用户从列表中选择一名学生时,会弹出一个日记输入框。当他们填写日记条目弹出框并单击提交按钮时,我需要将日记条目输入到JournalEntries表中,该表与从datagrid中选择的学生id关联 protected void SearchGrid_SelectedI

我有一个包含两个表的数据库;StudentID是“StudentList”表上的主键,StudentID是“JournalEntries”表上的外键

在我的网站上,我有一个搜索功能,用户可以从学生列表中搜索学生,结果显示在数据网格中。当用户从列表中选择一名学生时,会弹出一个日记输入框。当他们填写日记条目弹出框并单击提交按钮时,我需要将日记条目输入到JournalEntries表中,该表与从datagrid中选择的学生id关联

protected void SearchGrid_SelectedIndexChanged(object sender, EventArgs e)
{
    FNameLabel.Text = SearchGrid.SelectedRow.Cells[1].Text;
    LNameLabel.Text = SearchGrid.SelectedRow.Cells[2].Text;

    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Sites\Network2\nrsh\App_Data\myDB.mdb";
    string cmdstr = "SELECT StudentID FROM StudentList WHERE = SearchGrid.SelectedRow.Cells[3].Text AND INSERT into JournalEntries (Topic, SubTopic, Summary, Date, Notes) values (@Topic, @SubTopic, @Summary, @Date, @Notes)";

    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand com = new OleDbCommand(cmdstr, con);
    con.Open();
    //The following fields are added from the journal entry form to the corresponding database fields
    com.Parameters.AddWithValue("@Topic", ddlTopic.Text);
    com.Parameters.AddWithValue("@SubTopic", txtSubTopic.Text);
    com.Parameters.AddWithValue("@Date", txtDate.Text);
    com.Parameters.AddWithValue("@Summary", txtSummary.Text);
    com.Parameters.AddWithValue("@Notes", txtNotes.Text);
    com.ExecuteNonQuery();
    con.Close();
}

这是我对逻辑的看法,但可能是完全错误的,因为我对sql不太了解。任何帮助都很好。

不能将SELECT和INSERT语句组合在一起


字符串myValue=SearchGrid.SelectedRow.Cells[3]。文本


您没有SearchGrid.SelectedRow.Cells[3]的值。字符串中的文本..您实际上有SearchGrid.SelectedRow.Cells[3]。文本

string myValue = SearchGrid.SelectedRow.Cells[3].Text;
string cmdstr = "INSERT into JournalEntries (StudentID , Topic, SubTopic, Summary, Date, Notes) values (@StudentID , @Topic, @SubTopic, @Summary, @Date, @Notes)";
后来呢

com.Parameters.AddWithValue("@StudentID", myValue);
com.Parameters.AddWithValue("@Topic", ddlTopic.Text);
com.Parameters.AddWithValue("@SubTopic", txtSubTopic.Text);
com.Parameters.AddWithValue("@Date", txtDate.Text);
com.Parameters.AddWithValue("@Summary", txtSummary.Text);
com.Parameters.AddWithValue("@Notes", txtNotes.Text);
我在猜。您应该给出表名、表中的所有列及其数据类型int、string等

编辑:重构

您应该创建一个接受参数的类。您不应该有数据库代码。。。。坐在GUI代码所在的位置

public class JournalEntriesManager
{

public static void InsertJournalEntry ( string studentID, string topic , string subTopic, DateTime DateOf  , string summary , string notes )
{
// Your code here
}

}
这就像分离关注点的第一级

但简而言之,您的GUI级代码应该从控件收集信息……在这种情况下,应该将这些信息传递给BusinessLogic JournalEntriesManager。
诸如此类。

您不能将多个语句与AND语句组合在一起,也就是将多个条件组合在一起。此外,您不能将一个表中的行与另一个表中的行神奇地关联起来,相反,JournalEntries表中应该有另一列,其中包含与条目关联的学生的ID。在JournalEntries表中插入新行时,将在该列中包含学生ID,这就是日记条目与学生关联的方式


此外,您的查询不能包含变量名,如SearchGrid.SelectedRow.Cells[3].Text,您必须将该变量的值直接插入查询文本中。请记住,您正在将此查询发送到数据库服务器的另一台服务器,而它不知道脚本中的变量是什么。

SearchGrid.SelectedRow.Cells[3]。文本是包含StudentDalSO的单元格。因此,当您对一条SQL语句进行故障排除时,如果该语句没有执行您认为应该执行的操作,不要把它嵌入到C代码中,使问题复杂化。只需打开SQLServerManagementStudio并直接输入查询即可。这样,您就可以专注于SQL本身,而不是它周围的所有C设备。从技术上讲,这是一个喷气式飞机数据库。这个程序叫做Access。我要让你知道,不要挑剔,而是要帮助你进行未来的谷歌/必应搜索。