使用以下程序在sql server 2005中使用ASP插入值。但我不知道如何连接asp与sql server 2005.但我使用以下程序我得到了error.how我可以解决这个问题吗?连接aspo和sql server 2005以插入记录的另一种方法是什么?
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>Untitled Page</title>
</head>
<body>
<%
dim con, sql_insert, data_source="XEONSERVER\\SQLEXPRESS;Initial Catalog=abcd; User Id=abcd;password=abcd;";
sql_insert = "Insert into register values ('" + TextBox1.Text + "')";
set con = Server.CreateObject("ADODB.Connection");
con.Open data_source;
con.Execute sql_insert;
con.Close;
set conn = Nothing;
%>
<form action="Default.aspx" method="post">
Your name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="submit" value="Submit" />
</form>
</body>
</html>
答案 0 :(得分:1)
我个人在我的DBHelper类中使用这组函数
您只需粘贴连接字符串而不是:“SettingsHelper.getConnection()”
public class DBHelper
{
public static void runQuery(string lc_cmd, LogHelper logger)
{
SqlConnection cn = new SqlConnection(SettingsHelper.getConnection());
cn.Open();
SqlCommand cmd = new SqlCommand(lc_cmd, cn);
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
if (logger != null)
{
logger.Write(e.ToString());
logger.Write(lc_cmd);
}
else
{
throw (e);
}
}
finally
{
cmd.Dispose();
cn.Dispose();
}
}
public static void runQuery(string lc_cmd)
{
runQuery(lc_cmd, null);
}
public static SqlDataReader GetSqlDataReader(string Query, int SQLTimeOutSeconds)
{
SqlConnection cn = new SqlConnection(SettingsHelper.getConnection());
cn.Open();
SqlCommand cmd = new SqlCommand(Query, cn);
cmd.CommandTimeout = SQLTimeOutSeconds;
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
public static DataTable GetDataTable(string Query, LogHelper logger)
{
SqlConnection conn = new SqlConnection(SettingsHelper.getConnection());
try
{
SqlDataAdapter a = new SqlDataAdapter(Query, conn);
DataSet s = new DataSet();
a.Fill(s);
return s.Tables[0];
}
catch (SqlException e)
{
if (logger != null)
{
logger.Write(e.ToString());
logger.Write(Query);
}
else
{
throw (e);
}
}
finally
{
conn.Dispose();
}
return null;
}
public static DataTable GetDataTable(string Query)
{
return GetDataTable(Query, null);
}
}