private void Form1_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connnectionString); //不同数据库间数据传递(因为主键设为自增所以没有复制ID)
string sql = "select count(*) from EnergyTB"; SqlCommand cmd = new SqlCommand(sql, connection);// 定义Command对象 int a;
//我想把行数传给变量a,接下来怎么写?
}
a= Convert.ToInt32(cmd.ExecuteScalar());
SqlCommand cmd = new SqlCommand(sql, connection);// 定义Command对象
SqlDataAdapter sda=new SqlDataAdapter (cmd);
DataTable dt=new DataTable();
sda.Fill(dt);
a=dt.Rows.Count;
{
SqlConnection connection = new SqlConnection(connnectionString); //不同数据库间数据传递(因为主键设为自增所以没有复制ID)
string sql = "select count(*) from EnergyTB"; SqlCommand cmd = new SqlCommand(sql, connection);// 定义Command对象 connection.Open(); object a = cmd.ExecuteScalar();
int ab = Convert.ToInt32(a);
MessageBox.Show(ab.ToString());
connection.Close();
}这种写法可以么
a=Convert.ToInt32(cmd.ExecuteScalar());
connection.Close();
{
SqlConnection connection = new SqlConnection(connnectionString); //不同数据库间数据传递(因为主键设为自增所以没有复制ID)
string sql = "select count(*) from EnergyTB"; SqlDataAdapter sda = new SqlDataAdapter(sql,mycon);
DataSet myds = new DataSet();
int a= myds.table.rows.count()
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connnectionString); //不同数据库间数据传递(因为主键设为自增所以没有复制ID)
string sql = "select count(*) from EnergyTB"; SqlCommand cmd = new SqlCommand(sql, connection);// 定义Command对象 int a= cmd.
//我想把行数传给变量a,接下来怎么写?
a=cmd.ExecuteScalar(); ----这样就可以了
}
while (dr.Read())
{ dr[0].ToString(); }方法很多
int ab = Convert.ToInt32(a);
MessageBox.Show(ab.ToString());
=>object obj = cmd.ExecuteScalar()??"0";
int a ;
if(int.TryParse(obj.ToString(),out a))
{
MessageBox.Show(a.ToString());
}
else
{
MessageBox.Show("转换失败");
}
a= Convert.ToInt32(cmd.ExecuteScalar());这样就可以了啊!!!
{
SqlConnection connection = new SqlConnection(connnectionString); //不同数据库间数据传递(因为主键设为自增所以没有复制ID)
string sql = "select count(*) from EnergyTB"; SqlCommand cmd = new SqlCommand(sql, connection);// 定义Command对象 connection.Open(); object a = cmd.ExecuteScalar();
int ab = Convert.ToInt32(a);
MessageBox.Show(ab.ToString());
connection.Close();
}这种写法是可以的