发布网友 发布时间:2022-04-21 20:05
共1个回答
热心网友 时间:2023-07-01 16:14
aspx最小代码连接数据库
<%@ Page language="c#" codePage="65001"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.SessionState" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="System.Collections" %>
<%
//更新街道交叉口经纬度信息到数据库,如果没有,则插入,有则更新
// 用法: street1=华山路 & street2=衡山路 & lng=11111&lat=4444
System.Web.HttpContext context = this.Context;
System.Text.StringBuilder sb=new System.Text.StringBuilder();
context.Response.AddHeader("Prama","no-cache");
context.Response.CacheControl="private";
context.Response.Expires=0;
context.Response.ContentType="text/html";
//获取参数
string street1=Request["street1"];
string street2=Request["street2"];
string lng =Request["lng"];
string lat =Request["lat"];
if(street1==null) {street1="";}
if(street2==null) {street2="";}
if(lng==null) {lng="";}
if(lat==null) {lat="";}
//获取数据
DataSet ds = new DataSet();
int r=0;
string sql="";
string msgsuccess="操作成功<br/>";
string msgerror="不成功,没有记录<br/>";
string conn = "server=(local);database=callmeal;Uid=sa;Pwd=yyy;";
SqlConnection con = new SqlConnection(conn);
sql=" select * from street";
context.Response.Write(sql);
SqlCommand _cmd=new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
da.Fill(ds);
con.Open();
_cmd.Connection =con;
//更新数据
sql=" update street set lng=11 where streetid=1"
_cmd.CommandText=sql;
r=_cmd.ExecuteNonQuery();
if(r==-1)
context.Response.Write(msgerror);
else
context.Response.Write(msgsuccess);
con.Close();
//context.Response.Write(sql);
//context.Response.Write(sb.ToString());
context.Response.Flush();
context.Response.Close();
%>
参考资料:开发者在线http://www.builder.com.cn/