陌上尘

JVAA学习笔记
posts - 2, comments - 1, trackbacks - 0, articles - 4

.NET与SQL SERVER数据库连接代码

Posted on 2008-06-25 22:18 陌上尘 阅读(1154) 评论(1)  编辑  收藏 所属分类: ASP.NET
         string struid = TextBox1.Text;
             
 
            //连接数据库
 
            string strconn ="server = 127.0.0.1;database=xsbook;uid=sa;pwd=";
            SqlConnection conn = new SqlConnection(strconn);         
            conn.Open();
 
            //查询
 
            string strsql= "Select * from 图书表 where 书名=  '"+struid +"'";
                Label1.Text = strsql;
            SqlCommand mycmd = new SqlCommand(strsql,conn); 
            SqlDataReader rd = mycmd.ExecuteReader();
 
               
            //显示查询结果
 
            DataGrid1.DataSource = rd;
            DataGrid1.DataBind();     
 
            //关闭连接
 
            conn.Close();
 
                ________________________________________________________________________________________                string struid = TextBox1.Text;
 
                        //连接数据库
            string strconn ="server =127.0.0.1;database=xsbook;uid=sa;pwd=";
            SqlConnection conn = new SqlConnection(strconn);
            conn.Open( ) ;
 
            //查询
                        string strsql ="Select * from 图书表";
                        SqlDataAdapter mycomand =new SqlDataAdapter(strsql,conn);
            DataSet ds = new DataSet();
            mycomand.Fill(ds,"图书表");
 
            //显示查询结果
 
            DataGrid1.DataSource = ds.Tables["图书表"].DefaultView;
            DataGrid1.DataBind();     
    
            //关闭连接
                        conn.Close();

Feedback

# re: .NET与SQL SERVER数据库连接代码  回复  更多评论   

2012-04-28 10:57 by 小笑
protected void Button1_Click(object sender, EventArgs e)
{
string sql = "Data Source=PC-201202251849;Initial Catalog=us;Integrated Security=True";

SqlConnection con = new SqlConnection(sql);
string strsql = "insert into user(id,name)values('" + TextBox1.Text +"','" + TextBox2.Text +",')";// + "'," + TextBox1.Text + ",'" + TextBox1.Text,mima,shiming
SqlCommand cmd = new SqlCommand(strsql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

只有注册用户登录后才能发表评论。


网站导航: