0

我正在尝试使用 webmethod(services) 和网站将一些字段插入本地 ms 访问数据库。我试过抬头,但似乎无法发现我哪里出错了。谁能告诉我我是否做得对。下面的代码不会将新数据添加到数据库中,也不会将我引导回请求的页面。

服务网络方法:

[WebMethod]
public void AddNewPosts(string postUserName, string postTitle, DateTime postMessagepostDateTime, int subTopicId, string postMessage)
{
    //Connection string for the datbase
    string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Forum.accdb;Persist Security Info=True";
    OleDbConnection myConn = new OleDbConnection(database);

    //Execute the query
    string queryStr = "Insert into Posts (TopicId, PostTitle, PostUserName, PostDateTime) VALUES (" + subTopicId + ",'" + postTitle + "','" + postUserName + "'," + postMessagepostDateTime + ")";

    // Create a command object 
    OleDbCommand myCommand = new OleDbCommand(queryStr, myConn);
    // Open the connection 
    myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
}

从我的网站调用上述方法:

 protected void btnSubmit_Click(object sender, EventArgs e)
{
    //string postUserName = Page.User.Identity.Name;
    string postUserName = "tom123";
    string postTitle = txtTitle.Text;
    string postMessage = txtMessage.Text;
    DateTime postDateTime = DateTime.Now;
    int subTopicId = int.Parse(Request.QueryString["id"]);

    Service fs = new Service();
    fs.AddNewPosts(postUserName, postTitle, postDateTime, subTopicId, postMessage);

    //Redirect back to the SubTopic page
    Response.Redirect("SubTopic.aspx?id=" + subTopicId.ToString());


}
4

1 回答 1

0

您可以尝试在日期时间字符串周围加上引号 queryStr = "插入帖子(TopicId、PostTitle、PostUserName、PostDateTime)VALUES (" + subTopicId + ",'" + postTitle + "','" + postUserName + "',' " + postMessagepostDateTime + "')";

于 2013-04-25T11:22:12.523 回答