asp.net连接sql2005,cs文件里直接书写的代码,纯手工操作,不在web.config里面进行配置。

来源:互联网 发布:win10安全软件推荐 编辑:程序博客网 时间:2024/05/14 22:53

 本文目的是用最基本的方法,在不使用web.config配置文件的asp.net与sql2005进行连接,对数据库进行操作。只是进行基本的初级学习为目的。连接数据库的方式很多种,大家可以用多种方法。

default.cs文件

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strConnection = "user id=sa;password=qwertyuiop;";
        strConnection += "Initial Catalog=WebSiteTest;Data Source=OPEN-EQIANG//SQLEXPRESS;";
        strConnection +="connect timeout=30;";

        string strSql = "insert into  tuser (id,username) values(4,'ddd')";

        SqlConnection conn = new SqlConnection(strConnection );


        try
        {          
        conn.Open();
        SqlCommand comm = new SqlCommand(strSql ,conn );
        int rowsReturn = comm.ExecuteNonQuery();
        Response.Write("--------OK-----------");
        }
        catch
        {
            Response.Write("---------NO-------------");
        }
      
    }
}

原创粉丝点击