首页 > 代码库 > C# 对SqlServer数据库增删改查

C# 对SqlServer数据库增删改查

http://www.worlduc.com/blog2012.aspx?bid=730767

用C#sqlserver实现增删改查

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {


            while (true)
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "server=505-03;database=ttt;user=sa;pwd=123";
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                string sql="";

                Console.WriteLine("增加请输入1;删除请输入2;修改请输入3;查找请输入4");
                int i = Convert.ToInt32(Console.ReadLine());
                string str = "";
                switch (i)
                {
                    case 1:
                        sql = "insert into ";
                        Console.WriteLine("请输入你要修改的表");
                        str = Console.ReadLine();
                        sql += str + "(";
                        Console.WriteLine("请输入你要修改的列名,用逗号隔开");
                        str = Console.ReadLine();
                        sql += str + ")";
                        Console.WriteLine("请输入相应的数据,用逗号隔开");
                        str = Console.ReadLine();
                        sql += " values (" + str+")";
                        break;
                    case 2:
                        sql = "DELETE FROM ";
                        Console.WriteLine("请输入表名");
                        str = Console.ReadLine();
                        sql += str;
                        Console.WriteLine("请输入删除的条件");
                        str = Console.ReadLine();
                        sql += " where " + str;
                        break;
                    case 3:
                        sql = "UPDATE ";
                        Console.WriteLine("请输入表名");
                        str = Console.ReadLine();
                        sql += str + " set ";
                        Console.WriteLine("请输入要修改的数据,如money=0,多个用逗号隔开");
                        str = Console.ReadLine();
                        sql += str;
                        Console.WriteLine("请输入要修改的条件");
                        str = Console.ReadLine();
                        sql += " where " + str;
                        break;
                    case 4:
                        sql = "select ";
                        Console.WriteLine("请输入你要查询的列名,多个用逗号隔开");
                        str = Console.ReadLine();
                        sql += str;
                        Console.WriteLine("请输入你要查询的表名,多个用逗号隔开");
                        str = Console.ReadLine();
                        sql += " from " + str;
                        break;

                }
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }

        }
    }
}

C# 对SqlServer数据库增删改查