C#&;Crystal Reports,加载数据库失败错误
但是当我尝试运行时,我得到了以下异常。 我尝试了各种解决方案,但都失败了 当然,我已经添加了启动代码。 请给我一个主意C#&;Crystal Reports,加载数据库失败错误,c#,sql-server,crystal-reports,C#,Sql Server,Crystal Reports,但是当我尝试运行时,我得到了以下异常。 我尝试了各种解决方案,但都失败了 当然,我已经添加了启动代码。 请给我一个主意 private void SearchButton_Click(object sender, EventArgs e) { SqlConnection con; string source = @"Data Source = 1.2.3.4; Initial Catalog = Sales; USER ID = user1
private void SearchButton_Click(object sender, EventArgs e)
{
SqlConnection con;
string source = @"Data Source = 1.2.3.4; Initial Catalog = Sales; USER ID = user1; PASSWORD = 87665;";
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from dbo.SS where ADM01F = '@sBox'", con);
cmd.Parameters.AddWithValue("@scBox", SearchTBox.Text);
SqlDataAdapter adt = new SqlDataAdapter("select * from dbo.SS where ADM01F = '@sBox'", con);
DataSet DS = new DataSet();
adt.Fill(DS);
ReportDocument cryRpt = new ReportDocument();
string reportpath = Application.StartupPath + "\\SNIReport.rpt";
cryRpt.Load(@"C:\SalesReport\SNIReport.rpt");
cryRpt.SetDataSource(DS);
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
我看到的第一件事
您不应该在参数中使用单引号。对于单引号,查询将它们视为一个参数,而不是一个参数。您应该在SqlDataAdapter
构造函数中使用该命令作为第一个参数,而不是创建另一个需要参数的sql查询
SqlCommand cmd = new SqlCommand("select * from dbo.SS where ADM01F = @sBox", con);
cmd.Parameters.AddWithValue("@scBox", SearchTBox.Text);
SqlDataAdapter adt = new SqlDataAdapter(cmd, con);
尽量不要使用AddWithValue
。使用.Add()
方法重载指定参数类型及其大小
不要忘记使用自动处理连接、命令和适配器,而不是手动调用.Close()
或.dispose()
方法
如上所述,您的SqlConnection
对象没有任何连接字符串。您可以使用以下参数创建对象:
SqlConnection con = new SqlConnection(source);
或者将其设置为
您得到了以下异常???异常在哪里?您没有编写异常。我没有看到您实例化
SqlConnection con=newsqlconnection(source)
?(并将连接字符串(source
)传递给con
实例。)
SqlConnection con;
con.ConnectionString = source;