从SQL表

时间:2016-06-29 11:50:55

标签: c# visual-studio sql-server-express

我的问题如下:我想从SQL Server表中获取一些记录并将其显示在表单中的文本框中。

我现在想要的是SELECT这些记录,但会使用WHERE条件对其进行过滤。

我在这里连接到我使用的数据库:

private void Form2_Load(object sender, EventArgs e)
{          
    try
    {
        objConnect = new DatabaseConnection();
        conString = Properties.Settings.Default.barcodeConnectionString;

        objConnect.connection_string = conString;
        objConnect.Sql = Properties.Settings.Default.SQL;

        ds = objConnect.GetConnection;
        MaxRows = ds.Tables[0].Rows.Count;              
    }
    catch (Exception err)
    {
        MessageBox.Show(err.Message);
    }

    try
    {
        objConnect = new DatabaseConnection();
        conString = Properties.Settings.Default.barcodeConnectionString;

        objConnect.connection_string = conString;
        objConnect.Sql = Properties.Settings.Default.SQL2;

        ds2 = objConnect.GetConnection;
        MaxRows2 = ds2.Tables[0].Rows.Count; 
    }
    catch (Exception err)
    {
        MessageBox.Show(err.Message);
    }

我抓住记录的地方:

private void NavigateRecords()
{
    dRow = ds.Tables[0].Rows[inc];                       

    if (textBox1.Text == dRow.ItemArray.GetValue(0).ToString())
    {
        textBox2.Text = dRow.ItemArray.GetValue(1).ToString();
        textBox3.Text = dRow.ItemArray.GetValue(9).ToString();
    }
}

private void NavigateRecordz()
{
    dRow2 = ds2.Tables[0].Rows[inc2];

    if (textBox1.Text == dRow2.ItemArray.GetValue(3).ToString())
    {
        textBox4.Text = dRow2.ItemArray.GetValue(10).ToString();
        textBox5.Text = dRow2.ItemArray.GetValue(1).ToString();
    }
}

在标签浏览解决方案中,在属性 - >下;设置我使用以下值获得了这两个属性:

SELECT * FROM DAT_SCOMPART 

并且像这样工作;

如果我尝试使用

SELECT * 
FROM DAT_SCOMPART 
WHERE SCO_UDC > 99999 

(我需要的过滤器)它不再返回记录。

关于如何过滤记录的任何想法?

代码已更正,这两个按钮点击事件正在运行:

private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (inc < MaxRows -1)
            {
                inc++;                
                NavigateRecords();                
                btnRefresh.PerformClick();
            }
            else
            {                
                inc = 0;                                
                textBox2.Focus();
            }                       

        }

            private void btnRefresh2_Click(object sender, EventArgs e)
        {
            if (inc2 < MaxRows2 - 1)
            {
                inc2++;
                NavigateRecordz();
                btnRefresh2.PerformClick();
            }
            else
            {
                inc2 = -1;
                textBox2.Focus();
            }
        }     

0 个答案:

没有答案
相关问题