2012-02-29 57 views
0

我正在使用以下程序在SQL Server 2005中使用ASP插入值,但我不知道如何连接asp与sql server 2005.但我使用下面的程序,我得到了错误,我可以解决这个问题吗?是另一种连接aspo和sql server 2005插入记录的方式吗?如何连接和插入与SQL Server 2005的ASP?

<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

这是一个asp.net问题或一个asp-classic问题? – AnthonyWJones 2012-02-29 14:26:20

+0

必须是ASP.NET,因为ASP会因使用而发愁; 你可以请删除ASP-Classic标记,因为它的误导 – cavillac 2012-02-29 14:29:24

回答

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); 
    } 
} 
+0

我只是在sql服务器数据库中插入记录。是我在我的asp中使用整个类DBHelper吗? – user 2012-02-29 09:40:28

+0

您可以在您的app_code目录中添加DBHelper.cs,然后调用 'DBHelper.runQuery(“Insert into register values('”+ TextBox1.Text +“')”);' 但是请注意潜在的漏洞SQL注入在你的应用程序 – 2012-02-29 09:41:13

+0

或当然你可以只使用它的一部分,因为你喜欢 – 2012-02-29 10:02:20