代码如下:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>sql数据库连接</title>
</head>
<script language="C#" runat="server">
public SqlConnection MyConnection;
public string Message;
protected void Page_Load(Object src,EventArgs E){
MyConnection=new SqlConnection("server=localhost;uid=sa;pwd=fluxay;database=book");
SqlCommand MySQLCommand=new SqlCommand("insert into aspbook(name,[date]) values('fluxay','2004-1-1')",MyConnection);
try
 {
 MyConnection.Open();
 MySQLCommand.ExecuteNonQuery();
 Message="已经添加了一条信息!";
 }
 catch
 {
 Message="<b>插入信息失败</b>"+E.ToString();
 }
 finally
 {
 MyConnection.Close();
 }
}
</script>
<body>
<% Response.Write(Message);%>
</body>
</html>
我使用连接数据库的时候能连接成功,可是这个页面执行sql语句的时候却不行,老是执行catch里的内容

解决方案 »

  1.   

    连接字符串写错了,"uid" 改为"user id"
      

  2.   

    这样试试:SqlCommand MySQLCommand=new SqlCommand("insert into aspbook([name],[date]) values('fluxay','2004-1-1')",MyConnection);
    应该是name关键字的问题!
      

  3.   

    uid这里应该没有问题,我能连接上数据库,就是执行sql语句的时候可能出了问题,
    baihecheng(百合) 的建议我也改了,也不行,仍然是执行catch里的内容
      

  4.   

    先换成这个看一下错误提示是什么
    catch(Exception xx)
     {
    Response.Write(xx.ToString());
     }
      

  5.   

    用这个insert into aspbook([name],[date]) values('fluxay','2004-1-1')SQLSever里去试一下!
    最好把MyConnection.Open();放到前面去
      

  6.   

    你将连接字符串server=localhost;uid=sa;pwd=fluxay;database=book改成
    data source=localhost;initial catalog=book;uid=sa;pwd=fluxay; 看看
      

  7.   

    要么照着百合的话去做,要么先把catch与finally语句去掉看看出什么错.