c# - sql语法问题或者代码问题? "Index was outside the bounds of the array."

标签 c# asp.net mysql sql html

我必须对我的数据库进行一些重构,它看起来像这样:

database

但我必须将 WallPosting 更改为:

WallPosting

现在我的问题是如何修复我的 sql 语法,以便我的代码再次工作,我现在做了一些手动输入,看看是否可以显示它们:

entries

FriendUserID 与用户表中的另一个 UserID 相关,该用户显然具有不同的图片和信息,但我不知道如何显示来自不同用户 atm 的并发 WallPosting。

我的代码创建一个动态div,为该div提供一个ID=用户ID,并输入名为wallpostings的墙贴消息,它获取存储的有关用户ID的信息并应用与该用户ID相关的图像,有什么办法可以做到这一点用sql改变?或者我已经走进了一条单行道? atm 我只是想看看是否可以让 populatewallposts select 语句起作用。

我的代码:

public partial class UserProfileWall : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            //It is a postback so check if it was by div click (NOT WORKING because the javascript isnt posting back)
            string target = Request["__EVENTTARGET"];
            if (target == "DivClicked")
            {
                string id = Request["__EVENTARGUMENT"];
                //Call my delete function passing record id
                using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
                {
                    cn.Open();
                    using (OdbcCommand cmd = new OdbcCommand("DELETE FROM WallPosting WHERE idWallPosting="+id, cn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        string theUserId = Session["UserID"].ToString();
        PopulateWallPosts(theUserId);
    }
    private void PopulateWallPosts(string userId)
    {
        using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
        {
            cn.Open();
            using (OdbcCommand cmd = new OdbcCommand("SELECT idWallPosting, wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN User u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
            {
                using (OdbcDataReader reader = cmd.ExecuteReader())
                {
                    test1.Controls.Clear();

                    while (reader.Read())
                    {
                        System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                        div.Attributes["class"] = "test";


                        div.ID = String.Format("{0}", reader.GetString(0));
                        string id = Convert.ToString(div.ID);
                        //store the div id as a string
                        Image img = new Image();
                        img.ImageUrl = String.Format("{0}", reader.GetString(2));
                        img.AlternateText = "Test image";

                        div.Controls.Add(img);
                        div.Controls.Add(ParseControl(String.Format("&nbsp&nbsp " + "{0}", reader.GetString(1))));
                        div.Attributes.Add("onclick", "confirm_delete(" + id + ");");
                        // send the div id to javascript
                        div.Style["clear"] = "both";
                        test1.Controls.Add(div);
                    }
                }
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string theUserId = Session["UserID"].ToString();
        using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
        {
            cn.Open();
            using (OdbcCommand cmd = new OdbcCommand("INSERT INTO WallPosting (UserID, Wallpostings) VALUES (" + theUserId + ", '" + TextBox1.Text + "')", cn))
            {
                cmd.ExecuteNonQuery();
            }
        }
        PopulateWallPosts(theUserId);
    }
}

我收到错误:索引超出了数组的范围。

using (OdbcCommand cmd = new OdbcCommand("SELECT wp.WallPostings, p.PicturePath FROM WallPosting wp INNER JOIN User u ON u.UserID = wp.FriendUserID INNER JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn))

最佳答案

始终存储发布者,即使墙的所有者是自己发布的。这将为您简化 SQL。

SELECT wp.WallPostings, p.PicturePath 
FROM WallPosting wp 
INNER JOIN [User] u ON u.UserID = wp.PostedByUserID 
INNER JOIN Pictures p ON p.UserID = u.UserID 
WHERE wp.UserID=" + userId + " 
ORDER BY idWallPosting DESC

如果是1比1,为什么图片要放在单独的表格中?

关于c# - sql语法问题或者代码问题? "Index was outside the bounds of the array.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5526062/

相关文章:

asp.net - 如何在启动时获取 HttpContext.Current.GetOwinContext()

c# - "System.ServiceModel.Primitives"- 找不到程序集或找不到依赖项

mysql - mysql中if条件的使用方法

java - 如何制作只能通过接口(interface)访问的类?

c# - 捕捉 IOException

asp.net - SignalR OnConnected和OnDisconnected不触发

php - 比较 Laravel 5 中两个不同表的时间戳

java - 在 Hibernate 4 中获取错误意外 token

c# - 在类之间传递参数 - 使用公共(public)属性或将属性类作为参数传递?

c# - 如何处理鼠标事件