注册 登录
编程论坛 ASP.NET技术论坛

VS2005;C#; 连接SQL数据库时,删除数据库中数据的SQL语法怎么写?

飞石 发布于 2007-12-31 19:59, 4920 次点击
开发环境:
Microsoft Visual Studio 2005
C#

我要做一个删除数据库中的例子,以前连接 Access的时候,删除新闻的那句SQL语法就正确。现在换成连接MSSQL数据后,VS 2005提示*号附近语法错误,请各位帮忙看看,连接SQL时,如何写删除的SQL语句。

连接Access数据库
string constring = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(".") + "//db.mdb";
        OleDbConnection con = new OleDbConnection(constring);

删除资料的SQL语句
        string sql = "delete * from news where id= " + cid;
此时能正确执行命令。

---------------------------------------------------------------------------------
连接SQL数据库后
 string constring = "provider=sqloledb;server=210.51.168.156;uid=web1772504;pwd=c0w0m1c4;database=www_togetherad_com";
        OleDbConnection con = new OleDbConnection(constring);

系统提示下面这句话的*号附近有语法错误
        string sql = "select * from news where id= " + cid;
6 回复
#2
akuboy19822008-01-01 20:25
string sql = "delete from news where id = ""+cid+""";

string sql = "select * from news where id = '"+cid+"'";
#3
akuboy19822008-01-01 20:31
连接MSSQL数据库引入命名空间:using System.Data.SqlClient;
SqlConnection con = new SqlConnection(constring);
#4
a55114192008-01-01 20:49
cc
using System.Data.SqlClient;
SqlConnection con = new SqlConnection("server=210.51.168.156;uid=web1772504;pwd=c0w0m1c4;database=www_togetherad_com");
Sqlcommand Sql_cmd=new Sqlcommand("delete from news where id="+cid+"")
Sql_cmd.executereader();
#5
yindayi2008-01-02 16:07
去掉*号就完!
#6
飞石2008-01-03 17:31
6楼才是正确答案,谢谢了!
#7
威龙嘉少2008-01-05 19:01
*去掉,你怎么查询全部数据啊,总不能把要查询的一个一个都写出来吧
要是里边有几十个字段,岂不是要累死啊
我感觉问题不应该出在那里,按2楼的写法应该没问题!
1