C# 不能使用c返回多个SQL行数据

C# 不能使用c返回多个SQL行数据,c#,sql,sql-server,C#,Sql,Sql Server,我有一个数据库,我在其中存储多行及其点、颜色、宽度等。我知道这些点正在被存储,因为我检查了SQL表,它就在那里。但是,当我尝试重新加载这些点时,它只会加载我存储的最后一行。我不明白这是为什么 private void opendbtestToolStripMenuItem_Click(object sender, EventArgs e) { try { using (SqlConnection conn = new SqlCo

我有一个数据库,我在其中存储多行及其点、颜色、宽度等。我知道这些点正在被存储,因为我检查了SQL表,它就在那里。但是,当我尝试重新加载这些点时,它只会加载我存储的最后一行。我不明白这是为什么

  private void opendbtestToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            using (SqlConnection conn = new SqlConnection("blahblabhblah; "))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM Line", conn))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Shape Line = new Line();            // New line
                            Line.readSQL();
                            shapeList.Add(Line);
                            Invalidate();

                        }
                    }
                }
                conn.Close();
            }

        }
        catch(Exception ex)

        {
            MessageBox.Show(ex.Message);
        }
        }
readSQL函数

public override void readSQL()
    {
        try
        {
            using (SqlConnection conn = new SqlConnection("blahblahblah; "))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM Line", conn))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader != null)
                        {
                            while (reader.Read())
                            {

                                // string s = (reader["ID"].ToString());
                                int x1 = Convert.ToInt32(reader["x1"]);
                                int x2 = Convert.ToInt32(reader["x2"]);
                                int y1 = Convert.ToInt32(reader["y1"]);
                                int y2 = Convert.ToInt32(reader["y2"]);
                                Pt1 = new Point(x1, y1);
                                Pt2 = new Point(x2, y2);
                                PenWidth = Convert.ToInt32(reader["Width"]);
                                PenColor = Color.FromArgb(Convert.ToInt32(reader["Color"]));

                            }
                        }
                    }
                }
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
           // MessageBox.Show(PenColor.ToString());
           // MessageBox.Show(PenWidth.ToString());
        }
    }
如何写入数据库

public override void writeSQL()
    {
        using (SqlConnection conn = new SqlConnection("blahblahblah "))
        {
            using (SqlCommand comm = new SqlCommand())
            {
                comm.Connection = conn;
                comm.CommandType = CommandType.Text;
                comm.CommandText = "INSERT INTO Line (x1,x2,y1,y2, Width, Color) VALUES (@val1, @val2, @val3, @val4, @val5, @val6)";
                comm.Parameters.AddWithValue("@val1", Pt1.X);
                comm.Parameters.AddWithValue("@val2", Pt2.X);
                comm.Parameters.AddWithValue("@val3", Pt1.Y);
                comm.Parameters.AddWithValue("@val4", Pt2.Y);
                comm.Parameters.AddWithValue("@val5", PenWidth);
                comm.Parameters.AddWithValue("@val6", PenColor.ToArgb());
                try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                    MessageBox.Show("Insertion complete");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "fuuuu");

                }
            }
            conn.Close();
        }

    }

我想你应该试试这个。执行查询并将所有数据加载到datatable中。之后,您可以循环浏览每条记录

string query = "SELECT * FROM Line";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
    DataTable dt = new DataTable();
    dt.Load(cmd.ExecuteReader());  
    foreach (DataRow row in dt.Rows) 
    {
       //manipulate your data
    }   
}

将sqlDataReader发送到另一个函数并操作该函数中的数据,如下所示:

    public bool InitializeVariables(SqlDataReader sdr, bool isSingle = true)
    {
        try
        {
            if (sdr.HasRows)
            {
                if (isSingle)
                    sdr.Read();

                //initialize and manipulate data here.

                return true;
            }
            else
                return false;
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
根据您的代码,线是一个单一的对象。您正在对一个Line对象调用ReadSQL作为一个非静态方法,并期望它返回多行,但这并没有真正意义——您的方法在当前对象上设置了两个属性,因此根据定义,只返回集合中的最后一项

如果ReadSQL中的代码是为特定Id选择的,则它更有意义,例如

using (var cmd = new SqlCommand("SELECT * FROM Line Where id = @id", conn))
{
  cmd.Parameters.Add(new SqlParameter("Id", SqlDbType.Int) { Value = id });
  //....
}
如果要返回行集合,则需要在Line对象上使用静态方法,或者在其他位置使用返回行对象集合的方法

public static IEnumerable<Line> GetLines(int id)
{
    var returnData = new List<Line>();
    try
    {
        using (var conn = new SqlConnection("blahblahblah; "))
        using (var cmd = new SqlCommand("SELECT * FROM Line", conn))
        {
            conn.Open();

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    // string s = (reader["ID"].ToString());
                    int x1 = Convert.ToInt32(reader["x1"]);
                    int x2 = Convert.ToInt32(reader["x2"]);
                    int y1 = Convert.ToInt32(reader["y1"]);
                    int y2 = Convert.ToInt32(reader["y2"]);

                    returnData.Add(new Line()
                    {
                        Pt1 = new Point(x1, y1),
                        Pt2 = new Point(x2, y2),
                        PenWidth = Convert.ToInt32(reader["Width"]),
                        PenColor = Color.FromArgb(Convert.ToInt32(reader["Color"]))
                    });
                }
            }
            conn.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }


    return returnData;
}

创建一个对象并将其添加到一个集合中,其中分配了Pt1、Pt2?为什么有两个SqlConnection/Command/Reader?我已经更新了原来的帖子,让它更清晰一些。有两个sqlconnections/command/reader,因为它们位于不同的文件中。@文斯,为什么在主线程中调用readsql函数,是否需要再次读取sql?亲爱的@文斯,Shape Line=new Line!!!!这是什么???形状线=新形状!!哪一个?