C#中操纵SqlServer数据库

来源:互联网 发布:js字符串长度格式化 编辑:程序博客网 时间:2024/05/16 05:22
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//先打开类库文件
using System.Data.SqlClient;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {






           char[] name;
            int age;


            name = textBox1.Text.ToCharArray();
            age = System.Int32.Parse(textBox2.Text);
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=192.168.1.60;database=Test;uid=sa;pwd=123abc!";
            con.Open();


           /*
            SqlDataAdapter 对象。 用于填充DataSet (数据集)。
            SqlDataReader 对象。 从数据库中读取流..
            后面要做增删改查还需要用到 DataSet 对象。
            */


            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandType = CommandType.Text;
            com.CommandText = "INSERT INTO [Test].[dbo].[stu]([name],[age])VALUES('李',"+age+")";
            SqlDataReader dr = com.ExecuteReader();//执行SQL语
            
            dr.Close();//关闭执行
            con.Close();//关闭数据库


        }


        private void label1_Click(object sender, EventArgs e)
        {


        }
    }
}
0 0
原创粉丝点击