第一行:“=”附近有语法错误
各位大侠看哈下面的代码,当程序走到红色字体时时,就说第一行:“=”附近有语法错误,我怎么也看不出是哪错了,不知道那位能告知在下、、using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// CAN 的摘要说明
/// </summary>
public class CAN
{
public CAN()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection SqlCon()
{
SqlConnection mycon = new SqlConnection("Data Source=(local); User ID=sa;PWD=;DataBase=db_ComPany");
return mycon;
}
public bool ExecSqlcom(string SqlStr)
{
SqlConnection mycon = new SqlConnection("Data Source=(local);User ID=sa;PWD=;DataBase=db_ComPany");
mycon.Open();
SqlCommand cmd = new SqlCommand(SqlStr, mycon);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch
{ return false; }
finally
{
mycon.Close();
}
}
public bool ExecAdapterBind(GridView GridViewN, string SqlStr)
{
SqlConnection mycon = new SqlConnection("Data Source=(local);User ID=sa;PWD=;DataBase=db_ComPany");
mycon.Open();
SqlDataAdapter myada = new SqlDataAdapter(SqlStr, mycon);
DataSet myds = new DataSet();
myada.Fill(myds);
GridViewN.DataSource = myds;
try
{
GridViewN.DataBind();
return true;
}
catch
{
return false;
}
finally
{
mycon.Close();
}
}
public bool ExecAdapterBind2(DataList DataListN, string SqlStr)
{
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = "server=localhost;uid=sa;pwd=;database=db_ComPany";
mycon.Open();
SqlDataAdapter myada = new SqlDataAdapter(SqlStr, mycon);
DataSet myds = new DataSet();
myada.Fill(myds);
DataListN.DataSource = myds;
try
{
DataListN.DataBind();
return true;
}
catch
{
return false;
}
finally
{
mycon.Close();
}
}
public bool DataListDataKeyField(DataList DataListN, string SqlStr, string DKF)
{
SqlConnection mycon = new SqlConnection("Data Source=(local);User ID=sa;PWD=;DataBase=db_ComPany");
mycon.Open();
SqlDataAdapter myada = new SqlDataAdapter(SqlStr, mycon);
DataSet myds = new DataSet();
myada.Fill(myds);
DataListN.DataSource = myds;
DataListN.DataKeyField=DKF;
try
{
DataListN.DataBind();
return true;
}
catch
{
return false;
}
finally
{
mycon.Close();
}
}
public string SubStr(string sString,int nLeng)
{
if(sString.Length<=nLeng)
{
return sString;
}
int nStrLeng=nLeng-1;
string sNewStr=sString.Substring(0,nStrLeng);
sNewStr=sNewStr+"...";
return sNewStr;
}
public bool ExecAdapterBindInfoString(GridView GridViewN, string SqlStr, string DNK)
{
SqlConnection mycon = new SqlConnection("Data Source=(local);User ID=sa;PWD=;DataBase=db_ComPany");
mycon.Open();
SqlDataAdapter myada = new SqlDataAdapter(SqlStr, mycon);
DataSet myds = new DataSet();
myada.Fill(myds);
GridViewN.DataSource = myds;
GridViewN.DataKeyNames = new string[] { DNK };
try
{
GridViewN.DataBind();
return true;
}
catch
{
return false;
}
finally
{
mycon.Close();
}
}
public DataSet GetDataSet(string SqlStr, string tbName)
{
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = "server=localhost;uid=sa;pwd=;database=db_ComPany";
mycon.Open();
SqlDataAdapter xx = new SqlDataAdapter(SqlStr, mycon);
DataSet datast = new DataSet();
xx.Fill(datast , tbName);
return datast ;
}
} --------------------编程问答-------------------- 没看出来,帮顶! --------------------编程问答-------------------- 你在哪里调用的ExecAdapterBind2,设断点看下你的SqlStr的内容,拷到查询分析器里看下是否能正确执行,估计是你的SQL语句有问题 --------------------编程问答-------------------- 既然能编译,就应该不是c#语法错误,可能是"Data Source=(local); User ID=sa;PWD=;DataBase=db_ComPany"这个字符串有问题吧。你发更详细的错误信息让大家看看 --------------------编程问答-------------------- SQL有问题!绝对有问题!自己看着是调试。可能变量为空。。 --------------------编程问答-------------------- 看看SqlStr 的sql语句能直接在数据库中运行吗 --------------------编程问答-------------------- 应该是SqlStr中的sql语句有问题,建议调试一下,把sql语句放在sqlserver2005 中执行 --------------------编程问答-------------------- 很明显是SQL语句有问题
把调用这个方法的SQl语句贴出来看看 --------------------编程问答-------------------- 最关键的sql语句没有贴出来,谁能知道问题所在 --------------------编程问答-------------------- 显然是Sql问题 --------------------编程问答-------------------- 是不是因为pwd为空导致,sa的密码不能为空?
server=localhost;uid=sa;pwd=;database=db_ComPany --------------------编程问答-------------------- 代码真长,不想看完,,,,, myada.Fill(myds); 看看是不是 myada.Fill(myds,"表名"); 是不是缺少表名? --------------------编程问答--------------------
1.Data Source=.;Initial Catalog=db_ComPany;User ID=sa; Pwd=
2.Data Source=.;Initial Catalog=db_ComPany;Integrated Security=True
试试这2个 这个不行的话 应该就是你sql语句的问题啦 --------------------编程问答-------------------- myada.Fill(myds,"表名");
好像是少表名字啦 --------------------编程问答-------------------- 哦,对了,忘了告诉各位上面写 的只是个类文件,我在.cs文件里调用那个类,就出现上面的错误了
.cs文件code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class DownDefault : System.Web.UI.Page
{
CAN can = new CAN();
protected void Page_Load(object sender, EventArgs e)
{
string getid = Request["id"];
can.ExecAdapterBind2(DataList1, "select * from SoftView where id=" + getid);
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
can.ExecSqlcom("update tb_SoftwareInfo set number=number+1 where id=" + Request["id"]);
SqlConnection con = can.SqlCon();
con.Open();
SqlCommand com = new SqlCommand("select UpFileServerLoad from tb_SoftwareInfo where id=" + Request["id"], con);
string strUrl = Convert.ToString(com.ExecuteScalar());
Response.Redirect(strUrl);
}
}
--------------------编程问答-------------------- 13楼的那位大哥,我的程序还没跑到那呢、、 --------------------编程问答-------------------- 初步估计肯定是Request["id"]没有值,导致sql语句变成了
select * from SoftView where id=
=后面没有东西,所以出错 --------------------编程问答-------------------- 遇见,或= 附近有语法错误 一般先测试你的sql语句 --------------------编程问答-------------------- mycon.ConnectionString = "server=localhost;uid=sa;pwd=;database=db_ComPany";
改为
mycon.ConnectionString = "server=localhost;uid=sa;database=db_ComPany";
--------------------编程问答-------------------- 你应该把发生错误的代码用try catch 语句 包起来 然后看看抛出的什么错误信息。 --------------------编程问答-------------------- 看的眼花、、 --------------------编程问答-------------------- 应该是sql语句有问题正如上面说的没有得到ID的值 --------------------编程问答--------------------
can.ExecSqlcom("update tb_SoftwareInfo set number=number+1 where id=" + Request["id"]);
好大一个注入口啊.... --------------------编程问答-------------------- Request["id"]为null 这个原因
Request["id"]应该用Request.Form["id"] 界面传值 --------------------编程问答-------------------- 1,你用调试把执行的SQL语句拿出来放到数据库去执行看看成功不成功?
2,第一步成功的话看看数据库连接有没有问题 --------------------编程问答-------------------- server=localhost 如果是2005的话改成server=.\sqlexpress试下。 --------------------编程问答-------------------- 肯定是Sql有问题了,捕获一下Sql语句看看就是了 --------------------编程问答-------------------- string getid = Request["id"];
貌似这个值很有可能为空。。 --------------------编程问答-------------------- 应该是SQL语句出错了! --------------------编程问答-------------------- sql 语句错了,引发了SQL异常。 --------------------编程问答-------------------- SQL语句错误,没有搞到从DataNavigateUrlFormatString传来的变量值。
参考语句
--------------------编程问答-------------------- 郁闷。这代码
string SqlString;
string qsvalue;
qsvalue=Request.QueryString["id"];
SqlString="Select * From 表名 Where 字段='"+qsvalue+"'";
补充:.NET技术 , ASP.NET