急!!SQL语句在发送到数据库前的校验?检查基本的语法是否正确!!

shareach 2003-12-04 04:09:19
我想在SQL语句在发送到数据库前的对他进行校验,检查基本的语法是否正确!!
并且提取出SQL语句中的所有字段名、参数名和表名,如果是转换的字段名也提取出来
如:
select tab1.a1, tab1.a2 ,sum(tab2.a3+tab1.a4) countCol
from tab1,tab2
where a1=:param1 and tabl1.a2=:param2

我想提取的结果是:
字段:tab1.a1、tab1.a2 、countCol
表:tab1、tab2
参数::param1 、:param2

大家有什么好的经验和方法
...全文
264 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
shareach 2004-01-06
  • 打赏
  • 举报
回复
如果存在 select a,b,count(aa)from sdfa where ...怎么办,不讨论了揭帖,我基本解决了问题
dldl 2003-12-19
  • 打赏
  • 举报
回复
那就很简单了,吧"where","from" 的查找变成" where "," from "就行了。你的列名不会还有空格吧?
michaelowenii 2003-12-19
  • 打赏
  • 举报
回复
个人意见:我认为这样的“优化”,还不如没有优化 。你们想一想,最终你的程序代码,是什么呀?
shareach 2003-12-18
  • 打赏
  • 举报
回复
dldl 我开始的解析也和你差不多,烦的就是有的字段里面还有Where from等
Select PersonFrom,ComeWhere from ...
之类
dldl 2003-12-08
  • 打赏
  • 举报
回复
显示在3个listBox中的东西就是相应的列名,表名,参数值
dldl 2003-12-08
  • 打赏
  • 举报
回复
好了,问题解决了。
"select sum(col) c1,col1,col2 from t1,t2 where col=:par and col1=:par2;";
上面是我写的sql语句
下面的东西写入函数中调用就可以得到相应的东西了
int num1=str.IndexOf("where");
cols=str.Substring(num,num1-num);
cols=cols.Replace("from"," ").Trim();
string[] tab=cols.Split(',');
for(int i=0;i<tab.Length;i++)
{
this.listBox2.Items.Add(tab[i]);
}
cols=str.Substring(num1+6);
cols=cols.Replace("and",",");
cols=cols.Replace("or",",");
cols=cols.Replace(";"," ");
string[] pars=cols.Split(',');
for(int i=0;i<pars.Length;i++)
{

string sss=pars[i].Trim();
int j=sss.IndexOf("=");
sss=sss.Substring(j+1);
this.listBox3.Items.Add(sss);
}
yjmfdm 2003-12-05
  • 打赏
  • 举报
回复
我觉得还是用group by 来分开吧。
shareach 2003-12-05
  • 打赏
  • 举报
回复
我只找到一个SQLHelper.VB ,而且里面是一些SQL命令,
shareach 2003-12-05
  • 打赏
  • 举报
回复
NetAnt007(飞呀飞),你说的SQLHelper.cs在哪?
NetAnt007 2003-12-04
  • 打赏
  • 举报
回复
你自己看吧,太长了,
文件名字:SQLHelper.cs
NetAnt007 2003-12-04
  • 打赏
  • 举报
回复
hashtable,例子看petshop里面有一个函数专门提取字段的,
等我给你找一下。
dldl 2003-12-04
  • 打赏
  • 举报
回复
楼上给出的好像也不太对吧?如果a1&a2是联合主键呢?给出的值恰好可以确定一条纪录。所以他没有把表的结构给出,你就不能说对或不对。
qqqdong 2003-12-04
  • 打赏
  • 举报
回复
用SQLCommand的参数功能
不过你这个sql不对,应给为:
select tab1.a1, tab1.a2 ,sum(tab2.a3+tab1.a4) countCol
from tab1,tab2
where a1=:param1 and tabl1.a2=:param2
group by tab1.a1, tab1.a2

110,534

社区成员

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

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

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