"My ASP.Net script works on my server but it does not work on
your server."
MSSQL Connection
Sometimes, you may be wondering how to make your ASP.Net
script connect to the MSSQL database that you have just got from us. You might
have received information like this from us:
==========================================================
MsSQL2000 LOGIN INFORMATION
==========================================================
Host IP : 202.157.142.190
Database name : mydatabase_db
User name : mydbaseuser
Password : mydbpass
(Please use "SQL Server Authentication".)
==========================================================
There are many ways to connect to the database. Here is
just one example. The below is a simple example of how you can connect
to your database, using the information we gave you above, do a simple
query, and display the results.
<%@Page Language="C#"
ResponseEncoding="utf-8" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
protected void DisplayData() {
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader rd = null;
try {
con = new SqlConnection("server=202.157.142.190;uid=mydbaseuser;" +
"pwd=mydbpass;database=mydatabase_db");
cmd = new SqlCommand("SELECT * FROM mytable", con);
con.Open();
rd = cmd.ExecuteReader();
while(rd.Read()) {
Response.Write("<tr><td>");
Response.Write(rd.GetString(0));
Response.Write("</td><td>");
Response.Write(rd.GetString(1));
Response.Write("</td></tr>");
}
} catch (Exception e) {
Response.Write("<p><font color=\"red\">Err: ");
Response.Write(e.Message);
Response.Write("</font></p>");
} finally {
if(rd != null)
rd.Close();
if(con != null)
con.Close();
}
}
</script>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
</head>
<body>
<table>
<% DisplayData(); %>
</table>
</body>
</html>
|