为什么有的表所有的数据都可以读出来,有的表的值只能读出其中的一列? 看看表的数据有什么特殊的,比如null,等等 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好吧,我重贴public void DataCoherent1(){ //--1//string aa=null;string strCol=null;//存列的名string strColValue=null;//存列的alues值string sKeyName=null;//存主键的字段名string sKNValue=null;//存主键的valuesstrDSN=LinkSqlData();sAccDsn=LinkAccData();//DataTable AccDT = new DataTable();string[] sFldNam;int iFldCount;int iRcdCnt;int iTbl=Convert.ToInt32(GetTblCount());//取得表个数for(int i=0;i<iTbl;i++) {//--2 string sTblName=GetTblName(i);//取得表的名称 iRcdCnt=GetRecordCount(sTblName);//取得表里记录的个数; OleDbConnection AccConn=new OleDbConnection(sAccDsn); string strAccSQL = "Select * from "+sTblName+" order by Utime_bak"; OleDbDataAdapter AccDA= new OleDbDataAdapter(strAccSQL,AccConn); AccDA.Fill(AccDT); iFldCount=Convert.ToInt32(GetFldNameCount(sTblName));//取得字段个数 sFldNam=new string[iFldCount+3]; for(int iR=0;iR<iRcdCnt;iR++)//记录里的循环 {//--3 strCol=null; strColValue=null; for(int j=0;j<iFldCount;j++)//记录的列的循环 {//--4 sFldNam[j]=GetFldName(sTblName,j); //取得字段名sql的。access多三个state_bak,Utime_bak,CurUserName_bak if(j==iFldCount-1) {//--5 strCol+=sFldNam[j].ToString(); strColValue+= "'"+AccDT.Rows[iR][sFldNam[j].ToString()]+"'"; }//--5 else {//--6 strCol+=sFldNam[j]+",";//取得列名的串 strColValue+= "'"+AccDT.Rows[iR][sFldNam[j].ToString()] .ToString()+"'"+","; //取得列的values的值 }//--6 // aa=sFldNam[j]; }//-4 sFldNam[iFldCount]="state_bak"; sFldNam[iFldCount+1]="Utime_bak"; sFldNam[iFldCount+2]="CurUserName_bak"; sKeyName=GetKeyName(sTblName).Trim(); //取得当前表的主键名--- sKNValue=AccDT.Rows[iR][sKeyName].ToString(); //取得主键的value-- switch(AccDT.Rows[iR]["state_bak"].ToString()) {//---s-------连接到sql case "I_N"://插入新的记录,先插入主表,再插入从表 InsertRToSQL(sTblName,strCol,strColValue); break; case "U_O"://删除update的原记录,先删除从表,再删除主表 delRFromSQL(sTblName,sKeyName,sKNValue); break; case "U_N"://插入update后的记录 InsertRToSQL(sTblName,strCol,strColValue); break; case "D_O"://删除的记录 delRFromSQL(sTblName,sKeyName,sKNValue); break; }//---s }//--3 }//--2 }//--1 既然这样,你应该检查iRcdCnt=GetRecordCount(sTblName);//取得表里记录的个数;是不是这个函数的问题,使你得到的iRcdCnt有时返回值为1,致使你的循环只执行了一次呢?你没有贴出这个函数,所以没有办法判断了。 大家帮忙看看吧,我实在是晕了。要是所有的表不行,还好些,现在是有的可以,有的不可以。同事有的说是代码的问题,有的说是sql不支持access的一些字段的原因,可我把这个access里的东西删的就剩下char还是不行(再注:别的表是可以的),也不知道是这个表有原因,还是别的问题。真的是难受。本来想要推倒重来,但想到要建触发器就发憷。急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 这个iRcdCnt=GetRecordCount(sTblName);//取得表里记录的个数;取出来的数是正确的,不正确的原因是,在取列的values值时,只能取的一个列的values,别的都是空的。这样添加到数据库中也就只有一列加上了,别的列值就没 iFldCount=Convert.ToInt32(GetFldNameCount(sTblName));//取得字段个数???函数?贴出来 lemong(風之影):null有影响读取数据吗?可是我在所有的项里都添加了内容。 public string GetFldNameCount(string strTblName) { //string strDSN=LinkSqlData(); string strSQL="select Count(*) as aaaCount from sysobjects t1,syscolumns t2 where t1.id=t2.id and t1.xtype='u' and t1.name='"+strTblName+"'"; SqlConnection myConn = new SqlConnection(strDSN); SqlCommand myComm=new SqlCommand(strSQL,myConn); myConn.Open(); string R_str=myComm.ExecuteScalar().ToString(); myConn.Close(); return R_str; } //---------连接SQL数据库---------- public string LinkSqlData() { string strSqlDSN="Persist Security Info=False;Data Source=localhost;Initial Catalog=tjgp1;User ID=sa;Password=;"; return strSqlDSN; } 我这里运行是没有问题的,你检查下是否表的列名或表名取了关键字的名字另外你上面的//string strDSN=LinkSqlData();所以取到的strDSN可能不正确。 我在使用前做了一个调用,那个在哪个datacoherence1()的前面定义的。还真的是有关键字,取了一个name。 我把这个name的名字改了,还是一样的问题,也不知道是什么原因? 你在SQL查询分析器中运行select Count(*) as aaaCount from sysobjects t1,syscolumns t2 where t1.id=t2.id and t1.xtype='u' and t1.name=' '看看结果是多少,并检查出问题的表和列 是不是那个table没有清空的原因?那我要再次fill的话,我怎样清空呢? dataset.Tables["datatablename"].Clear();datatablename.Clear(); GetFldName函数的代码呢,建议把所有相关的代码贴出来,另外,最好不要使用这么多的内签循环,出了问题很难查的 谢谢大家,这个问题已经解决了就是没有清空table的原因 ~~~~~~~~急求助: Delphi的代码翻译成C#代码 ~~~~~~~~~~ |zyciis| 用WebService远程取出来的表,如何和本地SQL数据库关联查询呢? 急 谢谢 问一下序列化和反序列化的作用! C# 中做安装项目时碰到的问题 字符串保存为xml文件 提示出错,困扰了我3天了! 实学取经 请问判断一个字符串全为数字的正侧表达式???? 请用过<<ASP.NET Web 站点高级编程>>一书的源码的大侠进来看看!谢谢! 关于窗口关闭的问题 求助一个Visual C# 水晶报表的问题 困惑很长时间的问题----数据库操作方式的选择 MDI子窗口的焦点问题?
public void DataCoherent1()
{ //--1
//string aa=null;
string strCol=null;//存列的名
string strColValue=null;//存列的alues值
string sKeyName=null;//存主键的字段名
string sKNValue=null;//存主键的values
strDSN=LinkSqlData();
sAccDsn=LinkAccData();
//DataTable AccDT = new DataTable();
string[] sFldNam;
int iFldCount;
int iRcdCnt;
int iTbl=Convert.ToInt32(GetTblCount());//取得表个数
for(int i=0;i<iTbl;i++)
{//--2
string sTblName=GetTblName(i);//取得表的名称
iRcdCnt=GetRecordCount(sTblName);//取得表里记录的个数;
OleDbConnection AccConn=new OleDbConnection(sAccDsn); string strAccSQL = "Select * from "+sTblName+" order by Utime_bak";
OleDbDataAdapter AccDA= new OleDbDataAdapter(strAccSQL,AccConn);
AccDA.Fill(AccDT); iFldCount=Convert.ToInt32(GetFldNameCount(sTblName));//取得字段个数
sFldNam=new string[iFldCount+3];
for(int iR=0;iR<iRcdCnt;iR++)//记录里的循环
{//--3
strCol=null;
strColValue=null;
for(int j=0;j<iFldCount;j++)//记录的列的循环
{//--4
sFldNam[j]=GetFldName(sTblName,j);
//取得字段名sql的。access多三个state_bak,Utime_bak,CurUserName_bak
if(j==iFldCount-1)
{//--5
strCol+=sFldNam[j].ToString();
strColValue+=
"'"+AccDT.Rows[iR][sFldNam[j].ToString()]+"'";
}//--5
else
{//--6
strCol+=sFldNam[j]+",";//取得列名的串
strColValue+=
"'"+AccDT.Rows[iR][sFldNam[j].ToString()]
.ToString()+"'"+",";
//取得列的values的值
}//--6
// aa=sFldNam[j];
}//-4
sFldNam[iFldCount]="state_bak";
sFldNam[iFldCount+1]="Utime_bak";
sFldNam[iFldCount+2]="CurUserName_bak";
sKeyName=GetKeyName(sTblName).Trim();
//取得当前表的主键名---
sKNValue=AccDT.Rows[iR][sKeyName].ToString();
//取得主键的value--
switch(AccDT.Rows[iR]["state_bak"].ToString())
{//---s-------连接到sql
case "I_N"://插入新的记录,先插入主表,再插入从表
InsertRToSQL(sTblName,strCol,strColValue);
break;
case "U_O"://删除update的原记录,先删除从表,再删除主表
delRFromSQL(sTblName,sKeyName,sKNValue);
break;
case "U_N"://插入update后的记录
InsertRToSQL(sTblName,strCol,strColValue);
break;
case "D_O"://删除的记录
delRFromSQL(sTblName,sKeyName,sKNValue);
break; }//---s
}//--3
}//--2
}//--1
iRcdCnt=GetRecordCount(sTblName);//取得表里记录的个数;
是不是这个函数的问题,使你得到的iRcdCnt有时返回值为1,致使你的循环只执行了一次呢?
你没有贴出这个函数,所以没有办法判断了。
本来想要推倒重来,但想到要建触发器就发憷。
急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
取出来的数是正确的,不正确的原因是,在取列的values值时,只能取的一个列的values,别的都是空的。这样添加到数据库中也就只有一列加上了,别的列值就没
函数?贴出来
{
//string strDSN=LinkSqlData();
string strSQL="select Count(*) as aaaCount from sysobjects t1,syscolumns t2 where t1.id=t2.id and t1.xtype='u' and t1.name='"+strTblName+"'";
SqlConnection myConn = new SqlConnection(strDSN);
SqlCommand myComm=new SqlCommand(strSQL,myConn);
myConn.Open();
string R_str=myComm.ExecuteScalar().ToString();
myConn.Close();
return R_str;
}
//---------连接SQL数据库----------
public string LinkSqlData()
{
string strSqlDSN="Persist Security Info=False;Data Source=localhost;Initial Catalog=tjgp1;User ID=sa;Password=;";
return strSqlDSN;
}
另外你上面的
//string strDSN=LinkSqlData();
所以取到的strDSN可能不正确。
还真的是有关键字,取了一个name。
select Count(*) as aaaCount from sysobjects t1,syscolumns t2 where t1.id=t2.id and t1.xtype='u' and t1.name=' '
看看结果是多少,并检查出问题的表和列
那我要再次fill的话,我怎样清空呢?
datatablename.Clear();