Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示datagridview与sql语句中的数据,什么是正确的还是更有效的_C#_Winforms_Visual Studio 2010_Ms Access - Fatal编程技术网

C# 显示datagridview与sql语句中的数据,什么是正确的还是更有效的

C# 显示datagridview与sql语句中的数据,什么是正确的还是更有效的,c#,winforms,visual-studio-2010,ms-access,C#,Winforms,Visual Studio 2010,Ms Access,因此,我显示了access DB中的数据,该数据库基于单击的点图像。我想出了两种不同的方法来做这件事,我很好奇哪一种更有效 以下是我目前做这件事的两种不同方式。如果你能推荐一个更好的解决方案,我很想听听 解决方案1使用绑定到access db的datagridview private void risk1_Click(object sender, EventArgs e) { pointid.Text = "7"; int numval = Convert.ToInt32(poi

因此,我显示了access DB中的数据,该数据库基于单击的点图像。我想出了两种不同的方法来做这件事,我很好奇哪一种更有效

以下是我目前做这件事的两种不同方式。如果你能推荐一个更好的解决方案,我很想听听

解决方案1使用绑定到access db的datagridview

private void risk1_Click(object sender, EventArgs e)
{
    pointid.Text = "7";
    int numval = Convert.ToInt32(pointid.Text);
    //To get the correct row
    int id = numval - 1;
    //Point Id of the clicked point
    textBox1.Text = id.ToString();
    pointlbl.Text = dataGridView1.Rows[id].Cells[2].Value.ToString();
    //Cat ID of the clicked point
    catId.Text = dataGridView1.Rows[id].Cells[5].Value.ToString();

    webBrowser1.DocumentText = dataGridView1.Rows[id].Cells[4].Value.ToString();
    webBrowser1.Document.ExecCommand("SelectAll", false, null);
    webBrowser1.Document.ExecCommand("Copy", false, null);
    using (RichTextBox box = new RichTextBox())
    {
        box.Paste();
        box.Rtf = box.Rtf.Replace("<>", "");
        this.htmlRichTextBox1.Rtf = box.Rtf;
    }

    pointPanel.Visible = true;
    homePanel.Visible = false;
}

您的应用程序易受SQL注入的影响。使用参数化查询。至于效率,请简要介绍您的应用程序。这将为您提供回答问题所需的所有数据。我了解如何使用参数,但我不明白为什么这是每个人的第一个假设。您是否能够将sql转换为int值,或者在这一点上它不会失败?否则,您似乎可以将sql注入任何文本框。这是我自己使用的windows应用程序,我不会注入sql来删除或更改表。这也是设置此设置的实践值。参数需要更多的代码。。。但是谢谢你提到这一点。至于评测你的应用程序,我不知道你在说什么。但我明白你的意思,我应该养成使用参数并正确操作的习惯,而不是试图节省几秒钟的时间。。。从现在起我会这么做的。
private void risk2_Click(object sender, EventArgs e)
{
    pointid.Text = "2";
    int numval = Convert.ToInt32(pointid.Text);

    //Setup Connection to access db
    string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Adam\\Desktop\\braithwaite.mdb";

    //declare Connection, command and other related objects
    OleDbConnection conGet = new OleDbConnection(cnString);
    OleDbCommand cmdGet = new OleDbCommand();

    //try
    //{
    //open connection
    conGet.Open();
    //String correctAnswer;

    cmdGet.CommandType = CommandType.Text;
    cmdGet.Connection = conGet;

    cmdGet.CommandText = "SELECT * FROM Points WHERE point_id = "+numval+"";

    OleDbDataReader reader = cmdGet.ExecuteReader();

    reader.Read();
    pointlbl.Text = reader["point"].ToString();
    webBrowser1.DocumentText = reader["Description"].ToString();
    conGet.Close();
    pointPanel.Visible = true;
    homePanel.Visible = false;
}