SqlConnection、ASP.net C# 的问题

发布于 2024-11-07 20:29:52 字数 691 浏览 1 评论 0原文

我有以下 C# 代码:

public string TargetDate()
{
    SqlConnection con = 
        new SqlConnection("Server=localhost;Database=Timer;Trusted_Connectopn=True");
    SqlCommand cmd = new SqlCommand("select * from Timer");
    con.Open(); 

    DataSet ds = new DataSet(cmd,con); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    da.Fill(ds); 
    con.Close(); 
}

但出现错误: new DataSet(cmd,con); ...

错误:CS1502:最好重载 方法匹配

'System.Data.DataSet.DataSet(System.Runtime.Serialization.SerializationInfo,

System.Runtime.Serialization.StreamingContext)' 有一些无效参数

可能是什么问题?

I have the following C# code:

public string TargetDate()
{
    SqlConnection con = 
        new SqlConnection("Server=localhost;Database=Timer;Trusted_Connectopn=True");
    SqlCommand cmd = new SqlCommand("select * from Timer");
    con.Open(); 

    DataSet ds = new DataSet(cmd,con); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    da.Fill(ds); 
    con.Close(); 
}

but I get error at the: new DataSet(cmd,con); ...

the error: CS1502: The best overloaded
method match for

'System.Data.DataSet.DataSet(System.Runtime.Serialization.SerializationInfo,

System.Runtime.Serialization.StreamingContext)'
has some invalid arguments

What is could be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ映画 2024-11-14 20:29:52

试试这个:

SqlConnection con = new SqlConnection
    ("Server=localhost;Database=Timer;Trusted_Connection=True");

SqlCommand cmd = new SqlCommand("select * from Timer", con);

con.Open();

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

con.Close(); 

这更好:

DataTable dataTable = new DataTable();
using(SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))
using(SqlCommand command = connection.CreateCommand())
{
    command.CommandText = "select * from Timer";
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    dataTable.Load(reader);
}

Try this:

SqlConnection con = new SqlConnection
    ("Server=localhost;Database=Timer;Trusted_Connection=True");

SqlCommand cmd = new SqlCommand("select * from Timer", con);

con.Open();

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

con.Close(); 

This is even better:

DataTable dataTable = new DataTable();
using(SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))
using(SqlCommand command = connection.CreateCommand())
{
    command.CommandText = "select * from Timer";
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    dataTable.Load(reader);
}
望她远 2024-11-14 20:29:52

您的数据集构造函数错误。试试这个

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(con);

You've got the wrong constructor for the DataSet. Try this

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(con);
千纸鹤 2024-11-14 20:29:52

您似乎混淆了构造函数:

尝试以下操作:

DataSet ds = new DataSet(); 

SqlDataAdapter da = new SqlDataAdapter(con); 

希望有帮助

It seems you have mixed up the constructors:

Try the following:

DataSet ds = new DataSet(); 

SqlDataAdapter da = new SqlDataAdapter(con); 

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文