我正在ASP.Net中建立一个论坛但是有一个小问题。
我有1位用户创建了一个主题,可以很好地写一篇帖子,但是如果其他用户登录,则不会将帖子插入数据库。它返回它确实没有插入任何内容。原始用户可以登录并仍然发布,但没有其他人可以。
这是我后面的代码
protected void addPostBtn_Click(object sender, EventArgs e)
{
//Define ADO.NET objects.
string insertSQL;
string topic = Request.QueryString["topicid"].ToString();
insertSQL = "INSERT INTO Posts (TopicID, PostBody, PUserID)"
+ "VALUES (@Topic, @NewPostText, @PUserID)";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(insertSQL, con);
// Try to open the database and execute the update
int added = 0;
try
{
cmd.Parameters.AddWithValue("@Topic", topic);
cmd.Parameters.AddWithValue("@NewPostText", newPostText.InnerText);
cmd.Parameters.AddWithValue("@PUserID", Session["User_ID"]);
con.Open();
added = cmd.ExecuteNonQuery();
lblResults.Text = "Your post has been added";
}
catch (Exception err)
{
lblResults.Text = "Error inserting record. " + err.Message;
}
finally
{
con.Close();
}
if (added > 0)
{
this.BindRepeater();
}
}
我根本没有任何错误。它说它提交的很好,但它不在数据库中,除非原始海报这样做。
修改
刚才意识到这与我的观点有关。这是我目前的观点,它正在阅读
SELECT dbo.Posts.PostBody, dbo.Posts.PostDate, dbo.Posts.PostID, dbo.[User].username, dbo.Topic.TopicID
FROM dbo.Topic RIGHT OUTER JOIN
dbo.Posts ON dbo.Topic.TopicID = dbo.Posts.TopicID LEFT OUTER JOIN
dbo.[User] ON dbo.Topic.TUserID = dbo.[User].UserID AND dbo.Posts.PUserID = dbo.[User].UserID
但它现在为其他用户名返回NULL
答案 0 :(得分:0)
视图需要如下
SELECT dbo.Posts.PostBody, dbo.Posts.PostDate, dbo.Posts.PostID, dbo.[User].username, dbo.Topic.TopicID
FROM dbo.Topic LEFT OUTER JOIN
dbo.Posts ON dbo.Topic.TopicID = dbo.Posts.TopicID LEFT OUTER JOIN
dbo.[User] ON dbo.Posts.PUserID = dbo.[User].UserID