C# 对SqlServer数据库增删改查

Posted want_Success

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 对SqlServer数据库增删改查相关的知识,希望对你有一定的参考价值。

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数据库增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Zhuo笔记:使用C#链接SQL数据库并进行增删改查操作

使用C#书写SQLite数据库增删改查语句(以及插入byte[]时遇到的问题总结)

json-server对数据进行增删改查操作

用C#语言实现与SQL server的连接并实现增删改查的功能

C#写一套最全的SQL server帮助类(包括增删改查)

java连接sql数据库,怎么实现增删改查?