我想插入一些字段到本地MS訪問數據庫使用webmethod(服務)和網站。我嘗試過查找,但似乎無法找到我出錯的地方。任何人都可以告訴我,如果我做對了。下面的代碼不會將新數據添加到數據庫中,也不會將我引導回所請求的頁面。插入SQL(訪問)不工作使用webmethods Asp.net
服務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());
}
你是說沒有錯誤信息?不知道更多可能確保您試圖插入的ID不在您的表中。 – Constanta 2013-04-25 11:06:15
你也應該看看ExecuteNonQuery()的返回值是什麼:int returnVal = myCommand.ExecuteNonQuery(); – Constanta 2013-04-25 11:09:11
@Constanta我傳遞的ID可以多次使用,這決定了該帖子需要插入哪個主題。有一個帖子ID是唯一的,它不影響該列。是的,根本沒有錯誤信息。它只是沒有做任何事情,當按鈕被點擊 – user123 2013-04-25 11:11:08