请问怎样从不同数据源中获取想要的东西?(*.xls和Sql Server 2000)在线等待!

jeall 2003-11-27 11:11:23
我想从aa.xls中的sheet表和Sql Server 2000中的RD表中获取想要的两个表中字段,
sheet.aid=RD.aid,获取 sheet.A1,sheet.A2,RD.R1,RD.aid
...全文
50 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeall 2003-12-01
  • 打赏
  • 举报
回复
我已用其他方法实现!

把两个数据源封装在dataset中。
以*.xls为主,利用
DataView obj_dv=ds.Tables["sheet"].DefaultView ; //*.xls
obj_dv.RowFilter="GroupCode="+ds.Tables["ocrd"].Columns["GroupCode"].ToString().Trim();//Sql Server

//数据绑定
this.DataGrid1.DataSource=obj_dv;
this.DataGrid1.DataBind();
jeall 2003-12-01
  • 打赏
  • 举报
回复
我已经利用其他方法做出来了。
有兴趣的请参考http://expert.csdn.net/Expert/topic/2500/2500650.xml?temp=.9715692
孟子E章 2003-11-27
  • 打赏
  • 举报
回复
分开查询
jeall 2003-11-27
  • 打赏
  • 举报
回复
无语,你给我的都是单独的数据源的,我希望能有一个两者结合的例子,或者是其他两者结合的例子!
gOODiDEA 2003-11-27
  • 打赏
  • 举报
回复
1、http://www.csharphelp.com/archives3/archive494.html?printable=yes

2、http://www.dotnetbips.com/displayarticle.aspx?id=4
jeall 2003-11-27
  • 打赏
  • 举报
回复
UP!
jeall 2003-11-27
  • 打赏
  • 举报
回复
我上述的方法中,不就是分别把它们放在了两个datatable中,
然后按照我的需要,你的意思是,不用建立父子关联关系就可以查询得到吗?这样的话,没有办法知道xls中的数据是跟sql中的那一条数据相对应?

请说得在详细些!
dldl 2003-11-27
  • 打赏
  • 举报
回复
把excel表中的数据读如一个datatable,把sqlserver表中的数据读入一个datatable,然后查询就可以了!
tjq_tang 2003-11-27
  • 打赏
  • 举报
回复
楼主您既然要实现联合查询,可以考虑写一个过程
该过程执行
1.将xls倒入sql server的一个临时表中;
2.联合查询出相要的记录集合
将xls文件倒入sql server中可以参看
http://www.csdn.net/Develop/Read_Article.asp?Id=18623
对程序来说这个过程就是透明的
jeall 2003-11-27
  • 打赏
  • 举报
回复
我的程序思路:
using System.Data.SqlClient;
using System.Data.OleDb;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace YoungDC_Test
{
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(! this.Page.IsPostBack )
{
DataSet ds = new DataSet();

//Sql Server
string connString = ConfigurationSettings.AppSettings[0];
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter da = new SqlDataAdapter();

string sqlCat = "SELECT CardCode,CardName,CardType,GroupCode FROM ocrd";

da.SelectCommand = new SqlCommand(sqlCat, conn);;

da.Fill(ds, "ocrd");

//Excel
string filepath=Server.MapPath(".")+"/SBODemo_Chinese.xls" ;
string strCon="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+filepath.Trim()+";Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom="select GroupCode from [Sheet1$] as sheet";
myConn.Open();

OleDbDataAdapter Oda = new OleDbDataAdapter(strCom,myConn);

Oda.Fill(ds,"sheet");

myConn.Close();

//Relation
DataRelation relat = new DataRelation("SqlXls",
ds.Tables["ocrd"].Columns["GroupCode"],
ds.Tables["sheet"].Columns["GroupCode"]);

ds.Relations.Add(relat);

//合并成一个表,绑定导datagrid或其他显示工具都可以
DataTable ReTable=new DataTable();
ReTable.Columns.Add("CardCode",System.Type.GetType("System.Int32"));
ReTable.Columns.Add("CardName",System.Type.GetType("System.String"));
ReTable.Columns.Add("CardType",System.Type.GetType("System.Int32"));
ReTable.Columns.Add("GroupCode",System.Type.GetType("System.String"));

foreach (DataRow row in ds.Tables["ocrd"].Rows)
{
DataRow ReRow=ReTable.NewRow();

ReRow["CardCode"]=row["CardCode"].ToString();
ReRow["CardName"]=row["CardName"].ToString();
ReRow["CardType"]=row["CardType"].ToString();

DataRow[] childRows = row.GetChildRows(relat);
ReRow["GroupCode"]=childRows[3].ToString();
ReTable.Rows.Add(ReRow);
}

this.DataGrid1.DataSource=ReTable;
this.DataGrid1.DataBind();
}
}
jeall 2003-11-27
  • 打赏
  • 举报
回复
我现在可以查到了,但是在建立关联关系的时候,发生了错误,提示我
<父列和子列不具有类型匹配的列>

sheet.aid=RD.aid都是 int型

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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