代码如下:
<%@ 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里的内容
<%@ 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里的内容
解决方案 »
- ASP.NET如何实现用户上传头像、更改头像、头像保存、裁切等功能?
- 如何从数据库中读取一列并且绑定到一个DropdownList控件中
- treeview checkbox
- 很奇怪的“未将对象引用设置到对象的实例”,这个错很奇怪
- asp.net打印问题如何实现?报表?
- 主从表如何绑定
- 在asp.net内嵌表单冲突的问题
- 把xmldocment 对象转换成 string
- 求xml的学习过程
- 我们ASP.NET做的项目,现在想做成试用版,征求大家的方案,请大家踊跃发言!
- 我用asp.net如何将execl的数据导入到SQL-SERVER
- 求助:不小心把系统里边的aspnet用户给删掉了。改怎么修复呀!!
应该是name关键字的问题!
baihecheng(百合) 的建议我也改了,也不行,仍然是执行catch里的内容
catch(Exception xx)
{
Response.Write(xx.ToString());
}
最好把MyConnection.Open();放到前面去
data source=localhost;initial catalog=book;uid=sa;pwd=fluxay; 看看