首页 > 数据库技术 > 详细

C# 对SqlServer数据库增删改查

时间:2017-03-09 18:42:20      阅读:238      评论:0      收藏:0      [点我收藏+]

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

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数据库增删改查

原文:http://www.cnblogs.com/jingxuan-li/p/6526753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!