Console.WriteLine("按SELECT进行查看,按INSERT继续进行插入,按C退出操作"); a = Console.ReadLine(); if (a == "SELECT") { CK(); } if (a == "INSERT") { CR(); } if(a == "C") { return; }
}
public static void CK() { string a;
sqlSTR = "select * from UserDataTable_1"; SelectSQLDateBase(sqlSTR);
Console.WriteLine("按SELECT进行查看,按INSERT继续进行插入,按C退出操作"); a = Console.ReadLine(); if (a == "SELECT") { CK(); } if (a == "INSERT") { CR(); } if (a == "C") { return; } } public static void InsertSQLDateBase(string sqlSTR) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = ServeToDateBase; conn.Open();
SqlCommand cmd = new SqlCommand(sqlSTR, conn); int result = cmd.ExecuteNonQuery(); if (result > 0) { Console.WriteLine($"执行新增语句,受影响的行数{result}"); } conn.Close(); }
public static void SelectSQLDateBase(string sqlSTR) { SqlConnection con = new SqlConnection(); con.ConnectionString = ServeToDateBase; con.Open();
SqlCommand cmd = new SqlCommand(sqlSTR, con); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = cmd;
DataSet ds = new DataSet(); adapter.Fill(ds); con.Close(); DataTable dt = ds.Tables[0]; for (int i = 0; i < dt.Rows.Count; i++) { Console.WriteLine($"{dt.Rows[i]["userid"]} {dt.Rows[i]["userkeyword"]} {dt.Rows[i]["username"]}"); } } } }
public static void InsertSQLDateBase(string sqlSTR) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = ServeToDateBase; conn.Open();
SqlCommand cmd = new SqlCommand(sqlSTR, conn); int result = cmd.ExecuteNonQuery(); if (result > 0) { Console.WriteLine($"执行新增语句,受影响的行数{result}"); } conn.Close(); }