我如何将数据集值与整数进行比较
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 列的数据库,但我只比较了一个与整数提交的 "ex. if (submitid != dataset.submitted){ submitted =dataset.submitted}" 但出于某种原因,我无法让我的程序识别我的 dataset.I 正在尝试做的事情用 do while 或 for 循环,但我没有找到我遗漏的东西?谢谢 advance.I 知道有一些新手错误,但这些是可以修复的
使用从零开始的索引。
ds.Tables[0].Rows[0][0]
或
ds.Tables[0].Rows[0]["columnName"]
我认为最好使用 where 子句过滤数据集。这是实现您寻求的结果的一种更简单、更有效的方法(如果我理解正确的话)。我假设 [Submit ID] 被定义为数据库的整数,否则您将需要包含一个 CAST 语句,例如。 CAST ([提交 ID] AS int) - SQL-服务器语法
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 以改进您的代码(请参阅 "using" 声明。
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}" 但出于某种原因,我无法让我的程序识别我的 dataset.I 正在尝试做的事情用 do while 或 for 循环,但我没有找到我遗漏的东西?谢谢 advance.I 知道有一些新手错误,但这些是可以修复的
使用从零开始的索引。
ds.Tables[0].Rows[0][0]
或
ds.Tables[0].Rows[0]["columnName"]
我认为最好使用 where 子句过滤数据集。这是实现您寻求的结果的一种更简单、更有效的方法(如果我理解正确的话)。我假设 [Submit ID] 被定义为数据库的整数,否则您将需要包含一个 CAST 语句,例如。 CAST ([提交 ID] AS int) - SQL-服务器语法
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 以改进您的代码(请参阅 "using" 声明。