C#连接SqlServer以及插入数据的实例

来源:互联网 发布:asp 防sql注入 编辑:程序博客网 时间:2024/05/19 20:59
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;namespace test2{    class Program    {        static void Main(string[] args)        {            string connString = "Data Source=127.0.0.1;" +                "Persist Security Info=True;" +                "Initial Catalog=TestSql;" +                "Integrated Security=false;"+                "User ID=sa;"+                "Password=root123;";                          SqlConnection conn = new SqlConnection(connString);                          try            {                string insertCommand = "insert into employee (username,password) values('LiHua','123')";                conn.Open();                Console.WriteLine("open database successfully!!!");
                SqlCommand command = new SqlCommand(insertCommand,conn);                command.ExecuteNonQuery();
                       }            catch (Exception e)            {                Console.WriteLine(e.Message);                Console.WriteLine(e.GetType());                Console.WriteLine("connection Exception!!!");
            }            finally            {                conn.Close();                Console.WriteLine("close database successfully!!!");            }        }    }}

0 0