C# 對SqlServer資料庫增刪改查

來源:互聯網
上載者:User

標籤:[]   cmd   values   sys   條件   pwd   log   set   console   

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資料庫增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.