c# winform 水晶报表连接SQL数据的这段代码为什么有问题

jwy0126 2009-11-25 09:31:23
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;


namespace Report
{

public partial class ReportForm : Form
{
string conString = string.Empty;
FileStream fs = new FileStream(@"C:\ConnectString.txt",FileMode.Open,FileAccess.Read,FileShare.None);
StreamReader sr = new StreamReader(fs);
conString = sr.ReadLine();
SqlConnection conn = new SqlConnection(conString);

DataSet1 myData = new DataSet1();
SqlDataAdapter da;
public ReportForm()
{
InitializeComponent();
}


private void showReport()
{
da = new SqlDataAdapter("SELECT * FROM ReportTab", conn);
da.Fill(myData, "ReportTab");
//创建报表
CrystalReport1 cr = new CrystalReport1();
//设置报表的数据源
cr.SetDataSource(myData);
//设置报表视图的报表源
crystalReportViewer1.ReportSource = cr;
}


private void ReportForm_Load(object sender, EventArgs e)
{
//
showReport();
}
}
}

代码中 string conString = string.Empty;
FileStream fs = new FileStream(@"C:\ConnectString.txt",FileMode.Open,FileAccess.Read,FileShare.None);
StreamReader sr = new StreamReader(fs);
conString = sr.ReadLine();
SqlConnection conn = new SqlConnection(conString);
老是抛出一下问题。
错误 1 类、结构或接口成员声明中的标记“=”无效
错误 2 类、结构或接口成员声明中的标记“(”无效
错误 3 字段初始值设定项无法引用非静态字段、方法或属性“Report.ReportForm.fs”
错误 4 字段初始值设定项无法引用非静态字段、方法或属性“Report.ReportForm.conString”
...全文
129 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaojunquan 2009-12-28
  • 打赏
  • 举报
回复
不錯!!
阿泰 2009-11-25
  • 打赏
  • 举报
回复
呵呵。

jwy0126 感谢jwy0126的总结
jwy0126 2009-11-25
  • 打赏
  • 举报
回复
解决方法:
string conString = string.Empty;
FileStream fs = new FileStream(@"C:\ConnectString.txt",FileMode.Open,FileAccess.Read,FileShare.None);
StreamReader sr = new StreamReader(fs);
conString = sr.ReadLine();
SqlConnection conn = new SqlConnection(conString);

DataSet1 myData = new DataSet1();
SqlDataAdapter da;
应该放在函数体内。
jwy0126 2009-11-25
  • 打赏
  • 举报
回复
为什么 StreamReader sr = new StreamReader(fs);
conString = sr.ReadLine();
这里有问题? sr用“.”不能获取到这个对象的任何方法。ReadLine()是我自己手工敲的。
jwy0126 2009-11-25
  • 打赏
  • 举报
回复
ConnectString.txt 中内容是
Data Source=ZXH-JWY;Initial Catalog=CEC710;Integrated Security=True
就这一句话
cupwei 2009-11-25
  • 打赏
  • 举报
回复
呵呵 自己解决了啊
zhaozhijun0207 2009-11-25
  • 打赏
  • 举报
回复
阿泰激动了

4,818

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 图表区
社区管理员
  • 图表区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧