如何显示数据集的数量C# [英] How to show numbers of datasets C#

查看:118
本文介绍了如何显示数据集的数量C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在点击按钮后显示数据集的数量。但是,如果我单击按钮程序崩溃。错误消息是: ConnectionString属性未初始化



我的代码:



 private void button2_Click_1(object sender,EventArgs e)
{
this.timer1.Start();

SqlCeConnection conn;
SqlCeCommand cmd;
SqlCeDataReader rdr;

conn = new SqlCeConnection();
cmd = new SqlCeCommand(SELECT COUNT(*)FROM Pigeons,conn);
conn.Open();
MessageBox.Show(cmd.ExecuteScalar()。ToString());





我尝试过:



我在互联网上查找但我找不到任何解决方案。

解决方案

< blockquote>嗯,是的 - 它会。看代码:

 conn = new SqlCeConnection(); 
cmd = new SqlCeCommand(SELECT COUNT(*)FROM Pigeons,conn);
conn.Open();

您已经创建了一个连接对象,但是您没有告诉它您希望它连接到什么! SqlCE是一个基于文件的数据库:您必须至少指定要使用的文件,并将该信息提供给您的SqlCEConnection实例。

SQL Server Compact连接字符串 - ConnectionStrings.com [ ^ ]

 string strConnect = @Data Source = MyFolder\MyData.sdf; Persist Security Info = False ;; 
conn = new SqlCeConnection(strConnect);
cmd = new SqlCeCommand(SELECT COUNT(*)FROM Pigeons,conn);
conn.Open();


Hello I want to show the number of datasets after klicking a button. But if i click the Button the Program crashes. The error message is: The ConnectionString property was not initialized

My code:

private void button2_Click_1(object sender, EventArgs e)
        {
            this.timer1.Start();

            SqlCeConnection conn;
            SqlCeCommand cmd;
            SqlCeDataReader rdr;
                
                conn = new SqlCeConnection();
                cmd = new SqlCeCommand("SELECT COUNT(*) FROM Pigeons", conn);
                conn.Open();
                MessageBox.Show(cmd.ExecuteScalar().ToString());



What I have tried:

I looked up in the internet but I couldn't find any solution.

解决方案

Well, yes - it will. Look at the code:

conn = new SqlCeConnection();
cmd = new SqlCeCommand("SELECT COUNT(*) FROM Pigeons", conn);
conn.Open();

You have created a connection object, but you don't tell it what you want it to connect to! SqlCE is a file-based database: you have to at the very minimum specify the file you want to use and provide that info to your SqlCEConnection instance.
SQL Server Compact connection strings - ConnectionStrings.com[^]

string strConnect = @"Data Source=MyFolder\MyData.sdf;Persist Security Info=False;";
conn = new SqlCeConnection(strConnect);
cmd = new SqlCeCommand("SELECT COUNT(*) FROM Pigeons", conn);
conn.Open();


这篇关于如何显示数据集的数量C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆