|
.net öğrenmeye devam ancak dünyada çoğunlukla serverlarda linux apache mysql kombinasyonu hızı güvenilirliği için kulanıldığından database olarak mysql kulanmak en mantıklısı .net ile bir mysql databaseinden nasıl veri alırız veririz basit bir form application ile yapayım dedim şöyle birşey oldu.
1) visual studio da bir form application açıyoruz ardından içine 3 buton bir listbox ve 3 tanede texbox koyuyoruz. mysql den veri alıcaz veri yazıcaz ve veri silicez.

2) http://dev.mysql.com/downloads/connector/net/ adresinden visual studio için mysql connectorünü yükleyebiliriz bu eklenti sayesinde visual studioyadan mysqle bağlanıp ne olup bittiğini görebiliriz.
ayrıca http://www.mysql.com/ adresinden mysqli indirip bilgiasyarımızı kolayca kurabiliriz ardından ardından http://dev.mysql.com/downloads/workbench/5.2.html adresinden de görsel olarak şablon ve db ve tablo oluşturabilieceğimiz aracı indirip bir db oluşturabiliriz ben bu program için
net adında bir db users asında bir şablon ve integer usr_Id, string usr_name, string usr_pass adında tablolar oluşturdum.
3) yazmaya başlayaım yeni bir class dosyası yaratıyoruz ve içine databaseimize bağlanmamızı veri alıp verip silmemizi sağlayacak fonksiyonlarımızı tanımlıyoruz.
bu transver işlemleri için MySql.Data.MySqlClient kütüpanesini kullanacağız. using diyerek onuda codumuza ekledik.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MySql.Data.MySqlClient; using System.Windows.Forms;
namespace MysqlChat { public class DbWrapper { private MySqlConnection sqlConn; private string connStr; private bool isConnected;
public DbWrapper(string svr, string db, string user, string pass) { this.connStr = "Server=" + svr + ";Database=" + db + ";Uid=" + user + ";Pwd=" + pass + ";";
try { sqlConn = new MySqlConnection(this.connStr); } catch (Exception excp) { Exception myExcp = new Exception ("Error connecting you to " + "the my sql server. Internal error message: " + excp.Message, excp); throw myExcp; }
this.isConnected = false; }
public DbWrapper(string connStr) { this.connStr = connStr;
try { sqlConn = new MySqlConnection(this.connStr); } catch (Exception excp) { Exception myExcp = new Exception ("Error connecting you to " + "the my sql server. Error: " + excp.Message, excp);
throw myExcp; }
this.isConnected = false; }
public void Connect() { bool success = true;
if (this.isConnected == false) { try { this.sqlConn.Open(); } catch (Exception excp) { this.isConnected = false; success = false; Exception myException = new Exception ("Error opening connection" + " to the sql server. Error: " + excp.Message, excp);
throw myException; }
if (success) { this.isConnected = true; } } }
public void Disconnect() { if (this.isConnected) { this.sqlConn.Close(); } }
public bool IsConnected { get { return this.isConnected; } }
public void AddUser(string username, string password) { string Query = "INSERT INTO users(usr_name, usr_pass) values" + "('" + username + "','" + password + "')";
MySqlCommand addUser = new MySqlCommand(Query, this.sqlConn);
try { addUser.ExecuteNonQuery(); } catch (Exception excp) { Exception myExcp = new Exception ("Could not add user. Error: " + excp.Message, excp); throw (myExcp); } }
public MySqlDataReader GetUser() { MySqlDataReader Reader; string Query = "SELECT usr_Id, usr_name FROM users";
MySqlCommand GetUser = new MySqlCommand(Query, this.sqlConn);
try { Reader = GetUser.ExecuteReader(); return Reader; } catch (Exception excp) { Exception myExcp = new Exception ("Could not add user. Error: " + excp.Message, excp); throw (myExcp); }
}
public void DelUser(string ind) {
string Query = "DELETE FROM users WHERE usr_Id =" + ind;
MySqlCommand DelUser = new MySqlCommand(Query, this.sqlConn);
try { DelUser.ExecuteNonQuery(); } catch (Exception excp) { Exception myExcp = new Exception ("Could not add user. Error: " + excp.Message, excp); throw (myExcp); }
}
} }
4) form1.cs dosyamızda oluşturduğumuz classtan fonksiyonları kullanmaya başlayabiliriz.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient;
namespace MysqlChat { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { DbWrapper db = new DbWrapper("localhost", "net", "root", "123456"); db.Connect(); MySqlDataReader reader = db.GetUser();
while (reader.Read()) { string thisrow = ""; for (int i = 0; i < reader.FieldCount; i++) thisrow += reader.GetValue(i).ToString(); listBox1.Items.Add(thisrow);
} }
private void button2_Click(object sender, EventArgs e) { DbWrapper db = new DbWrapper("localhost", "net", "root", "123456"); db.Connect(); db.AddUser(textBox1.Text, textBox2.Text); }
private void button3_Click(object sender, EventArgs e) {
DbWrapper db = new DbWrapper("localhost", "net", "root", "123456"); db.Connect(); db.DelUser(textBox3.Text); }
} }
5) fonksiyonlar nasıl çalışıyor. ilk olarak
a) bağlantı fonksiyonu
private MySqlConnection sqlConn; private string connStr;
sqlConn adında işleri yürütecek bir obje oluşturduk. bu objemizin bir çok fonksiyonu var bunlardan bağlantı için biz.
sqlConn = new MySqlConnection(this.connStr); fonksiyonunu kullancağız (aslında bu bir constructor)
this.connStr = "Server=" + svr + ";Database=" + db + ";Uid=" + user + ";Pwd=" + pass + ";"; şeklinde kurduğumuz db in bilgilerini giriyoruz ve bağlantıyı kuruyoruz.
b) veri çekme, ekleme, silme Queryleri
veri almak için: string Query = "SELECT usr_Id, usr_name FROM users"; veri göndermek için:
string Query = "INSERT INTO users(usr_name, usr_pass) values" + "('" + username + "','" + password + "')";
silmek için:
string Query = "DELETE FROM users WHERE usr_Id =" + ind;
mysql querylerini direk olarak kullanarak verilerimizle işlemler yapabiliriz. tam liste için: http://www.w3schools.com/sql/default.asp
 pek başarılı bir anlatım olmadığının farkındayım sorularınız için emrahsari@live.com msn adresini kullanabilirsiniz.
|
|
|
6) The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural
numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first one
hundred natural numbers and the square of the sum.
int a = 0;
int b = 0;
for (int i=0; i < 101 ;i++){
a = a + i;
b = b + i * i;
}
a = a * a;
cout << a <<endl;
cout << a - b <<endl;
cout << b <<endl;
7) By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we
can see that the 6thprime is 13.
What is the 10001st prime number
int a = 3;
for (int i = 0; i<100000 ;i++){
if (i % 2 != 0 && i % 3 !=0 && i % 5 != 0 && i % 7 != 0)
{
a++;
if (a == 10001){
cout << a << "||" << i <<endl;
}
}
}
8) Find the greatest product of five consecutive
digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450
int x = 0;
int a;
char arr[1001] = {"7316713306"};
for(int i = 0; i <= 6; i++)
{
a = 1;
for(int j = 0; j <= 4; j++)
a = a * (arr[i+j] - '0');
if(a > x)
x = a;
}
cout<<x<<endl;
9) A Pythagorean triplet is a set of three natural
numbers, a < b < c, for which, a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for
which a + b + c = 1000. Find the product abc.
bool binmi(unsigned int, unsigned int, unsigned int);
bool kucukmu(unsigned int, unsigned int, unsigned int);
bool karesimi(unsigned int, unsigned int, unsigned int);
unsigned int a = 1;
unsigned int b = 1;
unsigned int c = 1;
while (karesimi(a,b,c) == 0){
while (a != 1000){
while (b != 1000){
if (binmi(a,b,c) == 1){
if (kucukmu(a,b,c) == 1){
if (karesimi(a,b,c) == 1){
cout << a<<" "<<b<<" "<<c<<" "<<endl;
}
}
}
b++;
}
a++;
b = 1;
}
c++;
a = 1;
}
bool binmi(unsigned int a, unsigned int b, unsigned int c){
if (a + b + c == 1000)
return 1;
else
return 0;
};
bool kucukmu(unsigned int a, unsigned int b, unsigned int c){
if ((a < b) && ( b < c))
return 1;
else
return 0;
};
bool karesimi(unsigned int a, unsigned int b, unsigned int c){
if (a*a + b*b == c*c)
return 1;
else
return 0;
};
10) The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
# include <stdio.h>
# include <conio.h>
# include <iostream.h>
long long arr[1000000],i,j;
long long suma;
main (){
for(i=1; i<1000000;i++)
arr=0;
i=2; arr[0]=1;
arr[1]=1;
while (i*i<1000000){
j=i;
while(i*j<1000000){
arr[i*j]=1;
j++;
}
i++;
while(arr==1)
i++;
}
suma=0;
for(i=1;i<=1000000;i++)
if(arr==0)
suma=suma+i;
i=0;
cout<<suma<<endl;
getch();
return 0;
}
|
|
2px; -webkit-border-vertical-spacing: 2px;">
Class Name: HowEasy Method Name: pointVal Parameters: String Returns: int TopCoder has decided to automate the process of assigning problem difficulty levels to problems. TopCoder developers have concluded that problem difficulty is related only to the Average Word Length of Words in the problem statement:
If the Average Word Length is less than or equal to 3, the problem is a 250 point problem. If the Average Word Length is equal to 4 or 5, the problem is a 500 point problem. If the Average Word Length is greater than or equal to 6, the problem is a 1000 point problem. Definitions: Token - a set of characters bound on either side by spaces, the beginning of the input String parameter or the end of the input String parameter. Word - a Token that contains only letters (a-z or A-Z) and may end with a single period. A Word must have at least one letter. Word Length - the number of letters in a Word. (NOTE: a period is NOT a letter)
The following are Words : "ab", "ab."
The following are not Words : "ab..", "a.b", ".ab", "a.b.", "a2b.", "."
Average Word Length - the sum of the Word Lengths of every Word in the problem statement divided by the number of Words in the problem statement. The division is integer division. If the number of Words is 0, the Average Word Length is 0. Implement a class HowEasy, which contains a method pointVal. The method takes a String as a parameter that is the problem statement and returns an int that is the point value of the problem (250, 500, or 1000). The problem statement should be processed from left to right. Here is the method signature (be sure your method is public): int pointVal(String problemStatement); problemStatement is a String containing between 1 and 50 letters, numbers, spaces, or periods. TopCoder will ensure the input is valid. Examples: If problemStatement="This is a problem statement", the Average Word Length is 23/5=4, so the method should return 500. If problemStatement="523hi.", there are no Words, so the Average Word Length is 0, and the method should return 250. If problemStatement="Implement a class H5 which contains some method." the Average Word Length is 38/7=5 and the method should return 500. If problemStatement=" no9 . wor7ds he8re. hj.." the Average Word Length is 0, and the method should return 250.
import java.util.Scanner;
public class HowEasy { public int pointVal(String s) { java.util.StringTokenizer st = new java.util.StringTokenizer(s, " "); int len = 0, w = 0; while (st.hasMoreTokens()) { String a = st.nextToken(); if (isWord(a)) { len+=a.length() - (a.charAt(a.length()-1)=='.' ? 1 : 0); w++; } } System.out.println("" + len + " " + w); int g = w == 0 ? 0 : len/w; if (g < 4) return 250; if (g > 5) return 1000; return 500; } boolean isWord(String s) { int i; for (i = 0; i < s.length(); i++) { if ((s.charAt(i) >= 'a' && s.charAt(i) <= 'z') || (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')) continue; if (i > 0 && i == s.length() - 1 && s.charAt(i) == '.') continue; return false; } return true; }
public static void main(String[] args) { HowEasy howEasy = new HowEasy(); String str = new String() ; Scanner in = new Scanner(System.in); str = in.nextLine(); System.out.println((howEasy.pointVal(str))); } }
|
|
1) If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
float a = 0;
for (float i = 1; i<1000; i++)
{
if (i % 3) == 0 || i % 5 == 0){
a = a + i;
} }
2) Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
unsigned __int64 a = 2;
unsigned __int64 b = 1;
unsigned __int64 c = 0;
for (unsigned __int64 i = 1; c<4000000; i++){
a = a + b;
b = a - b;
if (b % 2 == 0)
c = c + b;
}
3) The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
__int64 result = 0;
__int64 x = 600851475143;
__int64 t;
__int64 i = 1;
while (i != 10000){
i++;
t = get_prime(i);
if (x % t == 0){
result = t;
std::cout<<result<<std::endl;
}
}
__int64 get_prime(__int64 prm){
__int64 i_prm = 0;
for (__int64 i = 0; i<prm ; i++)
{
if (i % 2 != 0 && i % 3 !=0 && i % 5 != 0 && i % 7 != 0){
i_prm = i;
}
}
return i_prm;
}
4) A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
long int n,i,x;
for (n = 100; n != 1000; n++){
if (get_pal(n) == 1)
{
for (i = 100; i < 1000; i++){
if (get_pal(i) == 1){
x = i * n;
if (get_pal(x) == 1){
std::cout<<x<<"|x|";
}
}
}
}
}
bool get_pal(long int num){
long int n = num;
long int rev = 0;
for (long int i=0; i<=num; i++){
long int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
if(n == rev)
return 1;
else return 0;
}
5) 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?
int a = 1;
bool b = 0;
while (b == 0){
if ( a % 2 == 0 && a % 3 == 0 && a % 4 == 0 && a % 5 == 0 && a % 6 == 0 && a % 7 == 0 && a % 8 == 0 && a % 9 == 0 )
{
b = 1;
std::cout<<a<<"||";
}
a++;
}
|
|
http://student.cankaya.edu.tr/~c0411209/
adresinden 2.sınıf bilgisayar mühendisliği bölümü yaptığım asm ve c++ ödevlerine ulaşabilirsiniz. 3.sınıf yakında... uuuuu....
|
|
|
|
|
|
|
Page 1 of 2 |