ajax连接数据库并操作数据库

 Response.Write("<script  type='text/javascript' language='javascript' >alert('用户名不能为空!请输入!')</script>");

---------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" language="javascript">
        function showCustomer() {
            alert("ddd")
            var str = document.getElementById("txtvalue").value;
            if (str == "") {
                return;
            }
            var xmlhttp;

            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "Conn.aspx?name="+str, true);
            xmlhttp.send();
        }

        function add() {
            var names = document.getElementById("names").value;
            var Valuses = document.getElementById("Valuses").value;
            if (names == "") {
                return;
            }
            if (Valuses == "") {
                return;
            }
            var xmlhttp;
            if (window.XMLHttpRequest) {
              xmlhttp=new XMLHttpRequest();
             }
            else{
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
              xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("POST", "addweb.aspx", true);
              xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
              xmlhttp.send("names='" + names + "'&Valuses='" + Valuses + "'");
        }
    </script>
</head>
<body>
    <input type="button" id="btncoon" value="连接" onclick="showCustomer()" /><br />
    <input type="text" id="txtvalue" />
    <input type="button" id="Button1" value="查询" onclick="showCustomer()" />
    <br />
    <input type="text" id="names" /><input type="text" id="Valuses" />
    <input type="button" id="btnadd" value="添加" onclick="add()" />
    <div id="txtHint">
    </div>
</body>
</html>

------------------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Conn.aspx.cs" Inherits="WebApplication1.Conn" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%
        string strconn = "Data Source=PC-201001081513;Initial Catalog=WebFirst;Integrated Security=True";
        SqlConnection conn = new SqlConnection(strconn);
        SqlCommand cmd = new SqlCommand();
          if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        
             
        }
          string ad = Request["name"];
          string sql = "select Name,Valuses,ParentId from DaoHang where name='"+ad +"'";
          cmd.CommandText = sql;
          cmd.Connection = conn;
         // System.Data.DataSet ds = new System.Data.DataSet();
         // System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter(cmd);
         // sda.Fill(ds);
         // if (ds.Tables[0].Rows.Count > 0)
          //{
             
         // }
          SqlDataReader ar = cmd.ExecuteReader();
          while (ar.Read())
          {
              Response.Write(ar.GetSqlString(0)+"</br>");
          }
       
         
    %>
  
    </div>
    </form>
</body>
</html>

posted @ 2014-02-23 23:53  山水中华  阅读(21797)  评论(0编辑  收藏  举报