我正在尝试使用webmethod(服务)和网站将一些字段插入本地ms访问数据库。我试过抬头但似乎无法发现我出错的地方。任何人都可以告诉我,如果我做得对。下面的代码不会将新数据添加到数据库中,也不会将我引回到请求的页面。
服务Webmethod:
[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());
}
答案 0 :(得分:0)
您可以尝试使用日期时间周围的报价 string queryStr =“插入帖子(TopicId,PostTitle,PostUserName,PostDateTime)VALUES(”+ subTopicId +“,'”+ postTitle +“','”+ postUserName +“','”+ postMessagepostDateTime +“')”;