Just nu i M3-nätverket
Jump to content

Var anges databasen?


Tigers21

Recommended Posts

 

Var i denna koden anges det vilken databas som används?

 

Ska göra en gästbok och hittade detta script där man i ett antal fält lämnar uppgifter som sedan sparas i en databas.

 

Problemet är att jag inte vet var jag ska byta till min databas!

 

 

 

Hela koden för insert.aspx

 

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
<head>
<title>Inserting a Row of Data with Validation</title>
<link rel="stylesheet" href="style.css">

<style type="text/css">
#addPanel {
  width:90%; padding:10; background:khaki; 
  border:thin outset}
#posted {
  background:lightgreen; border:thin inset}
</style>

<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e ) {
  if ( !IsPostBack ) {
     addPanel.Visible = true;
     postPanel.Visible = false;
  }
  else {
     addPanel.Visible = false;
     postPanel.Visible = true;
  }
}

public void addMessage ( object src, EventArgs e ) {
  if ( Page.IsValid ) {
     SqlCommand myCmd = new SqlCommand ( );
     myCmd.Connection = new SqlConnection ( 
        ConfigurationSettings.AppSettings [ "aspa" ] );

     // define insert command parameters
     myCmd.CommandText = "INSERT INTO aspx_messages ( MessageDate, MessageFrom, Email, MessageSubject, MessageBody ) VALUES ( @Date, @From, @Mail, @Subject, @Body ) " + 
        "SET @Id = @@Identity";

     myCmd.Parameters.Add ( "@Date", SqlDbType.SmallDateTime ).Value = DateTime.Now;
     myCmd.Parameters.Add ( "@From", SqlDbType.NVarChar ).Value = msgFrom.Value;
     myCmd.Parameters.Add ( "@Mail", SqlDbType.NVarChar ).Value = msgEmail.Value;
     myCmd.Parameters.Add ( "@Subject", SqlDbType.NVarChar ).Value = msgSubject.Value;
     myCmd.Parameters.Add ( "@Body", SqlDbType.NText ).Value = msgBody.Value;

     // used for getting new record ID
     myCmd.Parameters.Add ( "@Id", SqlDbType.Int );
     myCmd.Parameters [ "@Id" ].Direction = ParameterDirection.Output;

     myCmd.Connection.Open ( );
     // do insert
     myCmd.ExecuteNonQuery ( );

     // fetch new record into reader
     myCmd.CommandText = "SELECT * FROM aspx_messages WHERE MessageId=" + 
        myCmd.Parameters [ "@Id" ].Value;
     postDetails.DataSource = myCmd.ExecuteReader ( CommandBehavior.SingleRow );
     postDetails.DataBind ( );
     myCmd.Connection.Close ( );
  }
}
</script>
</head>

<body>
<!-- #include virtual="~/shared/top.inc -->

<hr size=1 width=90%>

<br>

<center>

<asp:panel id="addPanel" runat="server">
  <h5>You can use this Guests database to add an entry.</h5>
  <form runat="server">

  <table width="85%" cellspacing=1 cellpadding=3 border=0>
  <col width=40% align="right">
  <col width=60%>
  <tr>
     <td>Your name:</td>
     <td><input id="msgFrom" runat="server">
        <asp:requiredfieldvalidator runat="server" 
           controltovalidate="msgFrom"
           errormessage="Sender's name cannot be blank. "
           display="none" /></td></tr>
  <tr>
     <td>Your email address:</td>
     <td><input id="msgEmail" runat="server">
        <asp:regularexpressionvalidator runat="server" 
           controltovalidate="msgEmail"
           validationexpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
           errormessage="Please enter a valid e-mail address. "
           display="none" /></td></tr>
  <tr>
     <td>Your subject:</td>
     <td><input id="msgSubject" runat="server"></td></tr>
  <tr>
     <td>Your message:</td>
     <td><textarea id="msgBody" rows=5 cols=35 runat="server" /></td></tr>
  </table>
  <p>
  <input type=submit value="Add New" runat="server" onServerClick="addMessage">
  <input type=reset value="Cancel" onClick="self.close ( )"></p>

  <asp:validationsummary runat="server" displaymode="SingleParagraph" 
     showmessagebox=true showsummary=false />

  </form>
</asp:panel>

<asp:panel id="postPanel" runat="server">
  <h5>Thank you for sharing your comments. This record has been added.</h5>

  <asp:datalist id="postDetails" width="85%" runat="server">

     <itemtemplate>
        <table id="posted" width=100% cellspacing=1 cellpadding=5 border=0>
        <col width=35% align="right">
        <tr>
           <td>MessageID:</td>
           <td><b><%# ( ( IDataRecord ) Container.DataItem ) [ "messageID" ] %></b></td></tr>
        <tr>
           <td>Date:</td>
           <td><b><%# DataBinder.Eval ( Container.DataItem, "messageDate", "{0:d}" ) %></b></td></tr>
        <tr>
           <td>Name:</td>
           <td><b><%# ( ( IDataRecord ) Container.DataItem ) [ "messageFrom" ] %></b></td></tr>
        <tr>
           <td>Email:</td>
           <td><b><%# ( ( IDataRecord ) Container.DataItem ) [ "Email" ] %></b></td></tr>
        <tr>
           <td>Subject:</td>
           <td><b><%# ( ( IDataRecord ) Container.DataItem ) [ "messageSubject" ] %></b></td></tr>
        <tr>
           <td>Message:</td>
           <td><b><%# ( ( IDataRecord ) Container.DataItem ) [ "messageBody" ] %></b></td></tr>
        </table>
     </itemtemplate>

  </asp:datalist>

</asp:panel>

</center>

<br>

<hr size=1 width=90%>

<!-- #include virtual="~/shared/viewsrc.inc" -->

</body>
</html>

 

 

[inlägget ändrat 2006-11-07 19:48:55 av Tigers21]

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...