Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将查询结果保存为int类型 受保护的无效按钮1\u单击(对象发送者,事件参数e) { con.Open(); sqlcommandcom4; string str5=“选择count(gid)作为[Committee].[dbo].[group];”中的计数; com4=新的SqlCommand(str5,con); SqlDataReader reader4=com4.ExecuteReader(); 整数计数; if(reader4.Read()) { count=(int)reader4[“count”].ToString; reader4.Close(); } 对于(int i=1;i_C#_Sql_Asp.net_Web - Fatal编程技术网

C# 如何将查询结果保存为int类型 受保护的无效按钮1\u单击(对象发送者,事件参数e) { con.Open(); sqlcommandcom4; string str5=“选择count(gid)作为[Committee].[dbo].[group];”中的计数; com4=新的SqlCommand(str5,con); SqlDataReader reader4=com4.ExecuteReader(); 整数计数; if(reader4.Read()) { count=(int)reader4[“count”].ToString; reader4.Close(); } 对于(int i=1;i

C# 如何将查询结果保存为int类型 受保护的无效按钮1\u单击(对象发送者,事件参数e) { con.Open(); sqlcommandcom4; string str5=“选择count(gid)作为[Committee].[dbo].[group];”中的计数; com4=新的SqlCommand(str5,con); SqlDataReader reader4=com4.ExecuteReader(); 整数计数; if(reader4.Read()) { count=(int)reader4[“count”].ToString; reader4.Close(); } 对于(int i=1;i,c#,sql,asp.net,web,C#,Sql,Asp.net,Web,因为,根据您的SQL语句和代码,您只关心第一列的第一行,您应该使用ExecuteScalar()从打开读卡器进行保存。然后Convert.ToInt32来处理转换。您的代码如下所示: protected void Button1_Click(object sender, EventArgs e) { con.Open(); SqlCommand com4; string str5 = "SELECT count(gid) as count

因为,根据您的SQL语句和代码,您只关心第一列的第一行,您应该使用
ExecuteScalar()
从打开读卡器进行保存。然后
Convert.ToInt32
来处理转换。您的代码如下所示:

 protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand com4;
        string str5 = "SELECT count(gid) as count from [Committee].[dbo].[group] ;";
        com4 = new SqlCommand(str5, con);
        SqlDataReader reader4 = com4.ExecuteReader();
        int count;
        if (reader4.Read())
        {
            count = (int) reader4["count"].ToString;
            reader4.Close();
        }

        for (int i = 1; i <= 6; i++) //(count instead of 6)
com4=新的SqlCommand(str5,con);
int count=Convert.ToInt32(com4.ExecuteScalar());

for(int i=1;i)运行此代码时会发生什么?我不能在循环中使用固定值,我需要从数据库中获取值,所以我在for循环中使用它。好的……从数据库中获取结果时会发生什么?
com4 = new SqlCommand(str5, con);
int count = Convert.ToInt32(com4.ExecuteScalar());

for (int i = 1; i <= count; i++){
    // Code here ... //
}