ITPub博客

首页 > > > 求助 关于 本机框架位于堆栈顶部,因此无法计算表达式的值

求助 关于 本机框架位于堆栈顶部,因此无法计算表达式的值

作者:deeply 时间:2019-09-10 15:01:23 0 删除 编辑

程序是点击屏幕上的按钮 返回数据库中MyClass表中数据的个数

代码如下:

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

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select count(*) from MyStudent";

            MessageBox.Show(SqlHelper.ExecuteScalar(sql, CommandType.Text).ToString());
        }
    }


其中SqlHelper是一个操作数据库的静态类,代码如下:

public static class SqlHelper
    {
        private static string conStr = ConfigurationManager.ConnectionStrings["Coco"].ConnectionString;
 public static object ExecuteScalar(string sqlStr, CommandType ct, params SqlParameter[] sq)
        {
            using (SqlConnection sqlCon = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sqlStr, sqlCon))
                {
                    cmd.CommandType = ct;
                    if (sq != null)
                    {
                        cmd.Parameters.AddRange(sq);
                    }
                    //打开连接
                    sqlCon.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }
}


连接字符串如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

  <connectionStrings>
    <add name="Coco" connectionString="Data Source=.;Initial Catalog=T_Seat;Integrated Security=True"/>
  </connectionStrings>
</configuration>

运行后,点击按钮后,查看错误信息,如下:

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1978/viewspace-3153704/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-05-30

  • 博文量
    215
  • 访问量
    24535