我认为我无法连接到我的数据库,因为它没有按预期更新。我不确定为什么会这样。 这是我的.aspx.cs文件:
public partial class addClass : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private static OleDbConnection GetConnection()
{
String connection;
connection = @"connectionstring";
return new OleDbConnection(connection);
}
string one = "one";
protected void submitButton_Click(object sender, EventArgs e)
{
OleDbConnection myConnection = GetConnection();
try
{
myConnection.Open();
String myQuery = "INSERT INTO Yoga Class([ClassName], [Level], [Duration], [Capacity], [Description]) values ('" +
classNameTextBox.Text + "','" + levelDropDownList.SelectedItem.Text + "','" + durationDropDownList.SelectedItem.Text + "','" + capacityDropDownList.SelectedItem.Text +
"','" + descriptionTextBox.Text + "');";
String myQuery2 = "INSERT INTO YogaTeacher([TeacherID]) values('" +one+ "');";
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection); //get rid of parameters
myCommand.ExecuteNonQuery();
myConnection.Close();
}
catch
{
Response.Write("Form did not submit due to an error");
}
finally
{
myConnection.Close();
}
}
}
}
我认为它没有更新的原因是因为表'Yoga Class'中的列teacherID是来自另一个表的外键。单击“提交”按钮时,将显示“catch”语句中的错误消息。我对么?如果我如何更改我的代码以使其工作?
任何帮助表示赞赏
谢谢
答案 0 :(得分:1)
您的代码: String myQuery =“INSERT INTO 瑜伽课 ... 表格名称“瑜伽课”不应该有空格。
答案 1 :(得分:0)
您的连接字符串无效。你将它设置为字符串“connectionstring”,它应该是一个完整格式化的连接字符串:例如
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Persist Security Info=False;"