无法从sqlclient.sqlcommand转换为字符串. [英] can not convert from sqlclient.sqlcommand to string..

查看:66
本文介绍了无法从sqlclient.sqlcommand转换为字符串.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
        {
            SqlConnection con = new SqlConnection(@"Data Source=Desk\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from Customers", con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd, con);
            da.Fill(dt);

            
        }
        catch (Exception e)
        {

        }
        return dt;



这是我的代码问题,无法从sqlclient.sqlcommand转换为字符串..
为什么此错误会得到pleese帮助我...........



this is my code problem occure with can not convert from sqlclient.sqlcommand to string..
why this error obtain plese help me ...........

推荐答案

引用
Refer here[^] for a list of the overloads you can use for the SqlDataAdapter. As for your error, you can try changing
SqlDataAdapter da = new SqlDataAdapter(cmd, con);


进入


into

SqlDataAdapter da = new SqlDataAdapter(cmd);



正如我在您的代码段中看到的那样,除cmd.ExecuteNonQuery之外,其他所有内容都很好.您可以使用ExecuteNonQuery执行目录操作(例如,查询数据库的结构或创建数据库对象,例如表),或通过执行UPDATEINSERT,或DELETE 声明.尽管ExecuteNonQuery不返回任何行,但是将使用数据填充任何输出参数或映射到参数的返回值.将其从代码中删除.它将正常工作.试试这个:
Hi,
As I can see in your code snippets everything is fine, except cmd.ExecuteNonQuery. You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements. Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data.. Remove it from your code. It''ll work properly. Try this:
SqlConnection con = new SqlConnection(@"Data Source=Desk\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Customers", con);
//cmd.ExecuteNonQuery();  -- Not required
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
da.Fill(dt);




祝一切顺利.
   -Amit




All the best.
    --Amit


嗨Sandip.
请编辑您的代码行

Hi Sandip.
Pleae edit your code line

SqlDataAdapter da = new SqlDataAdapter(cmd, con);  //incorrect



用这个



use this

SqlDataAdapter da = new SqlDataAdapter(cmd); //correct


因为您已经将SQL连接设置为SQLcommand

我认为它将为您提供帮助


because you have already set SQL Connection to SQLcommand

I think it will help you


这篇关于无法从sqlclient.sqlcommand转换为字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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