C#修改数据库,无法修改,求助!高手帮我看看是哪里的错误。
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSys...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace webtest.studyASPNET
{
public partial class useConnection : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionStr = "server = victor; uid = sa; pwd = 123;database = student"; //打开数据库
SqlConnection mySqlConnection = new SqlConnection(connectionStr);
//mySqlConnection.Open();
Response.Write("the connection is succsess!<br><br>");
string insertStr = "INSERT INTO information (UID,Name,Sex,Age,Address,Tel) VALUES ('008','王华','男','22','北京',‘11111’)";//ExecuteNonQuery 修改数据
SqlCommand myAddCommand = new SqlCommand(insertStr, mySqlConnection);
string deleteStr = "DELETE FROM information WHERE UID = '008'";
SqlCommand myDelCommand = new SqlCommand(deleteStr, mySqlConnection);
string Message = null;
try
{
mySqlConnection.Open();
myAddCommand.ExecuteNonQuery();
Message = "新纪录已修改到student 中的information";
}
catch (Exception ept)
{
Message = "can't insert the information<br>" + ept.ToString();
}
finally
{
Response.Write(Message + "<br>");
mySqlConnection.Close();
}
}
}
}
错误提示:
can't insert the information
System.Data.SqlClient.SqlException: ',' 附近有语法错误。 在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) 在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 webtest.studyASPNET.useConnection.Page_Load(Object sender, EventArgs e) 位置 C:\Documents and Settings\Administrator\桌面\GIF\C\webtest\webtest\studyASPNET\useConnection.aspx.cs:行号 36 展开
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace webtest.studyASPNET
{
public partial class useConnection : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionStr = "server = victor; uid = sa; pwd = 123;database = student"; //打开数据库
SqlConnection mySqlConnection = new SqlConnection(connectionStr);
//mySqlConnection.Open();
Response.Write("the connection is succsess!<br><br>");
string insertStr = "INSERT INTO information (UID,Name,Sex,Age,Address,Tel) VALUES ('008','王华','男','22','北京',‘11111’)";//ExecuteNonQuery 修改数据
SqlCommand myAddCommand = new SqlCommand(insertStr, mySqlConnection);
string deleteStr = "DELETE FROM information WHERE UID = '008'";
SqlCommand myDelCommand = new SqlCommand(deleteStr, mySqlConnection);
string Message = null;
try
{
mySqlConnection.Open();
myAddCommand.ExecuteNonQuery();
Message = "新纪录已修改到student 中的information";
}
catch (Exception ept)
{
Message = "can't insert the information<br>" + ept.ToString();
}
finally
{
Response.Write(Message + "<br>");
mySqlConnection.Close();
}
}
}
}
错误提示:
can't insert the information
System.Data.SqlClient.SqlException: ',' 附近有语法错误。 在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) 在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 webtest.studyASPNET.useConnection.Page_Load(Object sender, EventArgs e) 位置 C:\Documents and Settings\Administrator\桌面\GIF\C\webtest\webtest\studyASPNET\useConnection.aspx.cs:行号 36 展开
2个回答
展开全部
已经提示的很清楚了 北京后面那个逗号你打错了,应该是英文的逗号,
string insertStr = "INSERT INTO information (UID,Name,Sex,Age,Address,Tel) VALUES ('008','王华','男','22','北京',‘11111’)";//ExecuteNonQuery 修改数据
string insertStr = "INSERT INTO information (UID,Name,Sex,Age,Address,Tel) VALUES ('008','王华','男','22','北京',‘11111’)";//ExecuteNonQuery 修改数据
追问
再请问一下:它提示了那么长一段,如何看错误提示呢?
追答
一般来说主要看两点,一是错误类型,二是行号,比如上面的
can't insert the information
System.Data.SqlClient.SqlException: ',' 附近有语法错误。 这个一看就是sql语法问题,xx附近有语法错误就表示那个地方写错了。
再看行号,36行,36行是catch,错误捕获,那问题应该在上面try的里面,跟语法相关的那就只有myAddCommand.ExecuteNonQuery();,再向上检查到具体有sql语法的地方那就是 string insertStr = "INSERT IN这里了,再按前面的“xx附近有语法错误”就可以查到了。
当然你这个例子有些特别,一般来说看到',' 附近有语法错误,中间的这个中文的逗号,一般是不可能会在程序中用到的(除非是当做字符串或者是注释之类的),你可以直接查找,以定位错误
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询