如何将数据集值与整数进行比较

时间:2015-01-22 15:22:16

标签: c# sql visual-studio

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        int i=0;
        int submitid=0;
        int controlid;
        int counter = 0;
        string connetionString = null;
        SqlConnection connection ;
        SqlCommand command ;
        string sql = null;

        DataSet ds = new DataSet();

        connetionString = "Data Source=localhost;database=Starbulk;Integrated Security=True";
        sql = "SELECT * FROM Table_1";
         connection = new SqlConnection(connetionString);

            try
        {
            connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Submit ID] FROM Table_1", connection);
            command = new SqlCommand(sql, connection);
            adapter.SelectCommand = command;
            adapter.Fill(ds,"Table_1");
            int value = Convert.ToInt32(ds.Tables[1].Rows);



           do{
            if (submitid != value){

             MessageBox.Show(submitid.ToString());
            }

          }while (submitid > 0);


                 connection.Close();
        }
        catch (Exception )


{
            MessageBox.Show("Can not open connection ! ");
        }


    }


    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

}

嗨我有一个包含10列的数据库,但我只用一个整数来比较提交“ex.if(submitid!= dataset.submitted){submitted = dataset.submitted}”但由于某种原因我无法得到我的程序识别我的数据集。我正在尝试使用do while或for循环,但我没有找到我缺少的东西?提前感谢。我知道有一些新手错误,但那些是可修复的

2 个答案:

答案 0 :(得分:0)

使用从零开始的索引。

ds.Tables[0].Rows[0][0]

ds.Tables[0].Rows[0]["columnName"]

答案 1 :(得分:0)

我认为最好使用where子句过滤数据集。这是一种更简单,更有效的方法来实现您所寻求的结果(如果我理解正确的话)。我假设[提交ID]被定义为数据库的整数,否则您将需要包含一个CAST语句,例如。 CAST([提交ID] AS int) - SQL-Server语法

SELECT [Submit ID] FROM Table_1 WHERE [Submit ID] = 0

或将值设置为任何值使用参数

SELECT [Submit ID] FROM Table_1 WHERE [Submit ID] = @value

command.Parameters.Add(new SqlParameter("value", submitid));

我还建议您查看 this link以改进您的代码(请参阅"使用"声明。