[英]How can i compare a dataset value with an integer
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列的数据库,但是我只比较了一个整数。Submitted“例如,如果(提交!我的数据集。我正在尝试使用do while或for循环来做到这一点,但我没有发现我缺少的东西吗?谢谢,我知道有一些新手错误,但这些错误是可以修复的
使用从零开始的索引。
ds.Tables[0].Rows[0][0]
要么
ds.Tables[0].Rows[0]["columnName"]
我认为最好使用where子句过滤数据集。 这是获得所需结果的一种更简单,更有效的方法(如果我理解正确的话)。 我假设[Submit 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));
我还建议您查看此链接以改进您的代码(请参见“ using”语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.