C # change and delete SQL Server database

Source: Internet
Author: User
Tags readline

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

Using C#sqlserver to realize adding and deleting

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 ("Increase please enter 1; Delete Please enter 2; modify please enter 3; find Please enter 4");
int i = Convert.ToInt32 (Console.ReadLine ());
String str = "";
Switch (i)
{
Case 1:
sql = "INSERT INTO";
Console.WriteLine ("Please enter the table you want to modify");
str = Console.ReadLine ();
SQL + = str + "(";
Console.WriteLine ("Please enter the name of the column you want to modify, separated by commas");
str = Console.ReadLine ();
SQL + = str + ")";
Console.WriteLine ("Please enter the corresponding data, separated by commas");
str = Console.ReadLine ();
SQL + = "VALUES (" + str+ ")";
Break
Case 2:
sql = "DELETE from";
Console.WriteLine ("Please enter the table name");
str = Console.ReadLine ();
SQL + = str;
Console.WriteLine ("Please enter the conditions for deletion");
str = Console.ReadLine ();
SQL + = "where" + str;
Break
Case 3:
sql = "UPDATE";
Console.WriteLine ("Please enter the table name");
str = Console.ReadLine ();
SQL + = str + "set";
Console.WriteLine ("Please enter data to be modified, such as money=0, multiple separated by commas");
str = Console.ReadLine ();
SQL + = str;
Console.WriteLine ("Please enter the conditions to be modified");
str = Console.ReadLine ();
SQL + = "where" + str;
Break
Case 4:
sql = "Select";
Console.WriteLine ("Please enter the name of the column you want to query, multiple separated by commas");
str = Console.ReadLine ();
SQL + = str;
Console.WriteLine ("Please enter the name of the table you want to query, multiple separated by commas");
str = Console.ReadLine ();
SQL + = "from" + str;
Break

}
Cmd.commandtext = SQL;
Cmd.commandtype = CommandType.Text;
Cmd. ExecuteNonQuery ();
}

}
}
}

C # change and delete SQL Server database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.