Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
Thursday, November 23, 2017
Wednesday, November 22, 2017
Monday, March 7, 2016
Monday, October 20, 2014
Sunday, March 10, 2013
C# + SQL Example #1
* Gets max row count
* Inserts a new row (value number greater than max rows)
* Displays row values
[CODE]
string connString = "Server=localhost;Database=cshptest;Uid=root;"; // Select DB
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
// Command to count the rows
conn.Open();
command.CommandText = "SELECT COUNT(*) FROM csdat";
int rows = Convert.ToInt32(command.ExecuteScalar());
int newrows = rows + 1;
conn.Close();
// Insert a new data row, with the value greater than last index value (ex. 5 + 1 = 6)
command.CommandText = "Insert into csdat (id,dataT) values('" + newrows + "','" + newrows + "');";
conn.Open();
command.ExecuteNonQuery();
conn.Close();
MySqlDataReader reader;
// Read data in each row
for (int i = 0; i <= rows; i++)
{
string cnt = i.ToString();
conn.Open();
command.CommandText = "Select dataT from csdat where id=" + cnt; // Select column from table
reader = command.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(reader["dataT"].ToString()); // extract data from column
}
conn.Close();
}
[/CODE]
* Inserts a new row (value number greater than max rows)
* Displays row values
[CODE]
string connString = "Server=localhost;Database=cshptest;Uid=root;"; // Select DB
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
// Command to count the rows
conn.Open();
command.CommandText = "SELECT COUNT(*) FROM csdat";
int rows = Convert.ToInt32(command.ExecuteScalar());
int newrows = rows + 1;
conn.Close();
// Insert a new data row, with the value greater than last index value (ex. 5 + 1 = 6)
command.CommandText = "Insert into csdat (id,dataT) values('" + newrows + "','" + newrows + "');";
conn.Open();
command.ExecuteNonQuery();
conn.Close();
MySqlDataReader reader;
// Read data in each row
for (int i = 0; i <= rows; i++)
{
string cnt = i.ToString();
conn.Open();
command.CommandText = "Select dataT from csdat where id=" + cnt; // Select column from table
reader = command.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(reader["dataT"].ToString()); // extract data from column
}
conn.Close();
}
[/CODE]
Monday, June 4, 2012
C# Database Programming Start-up Guide. Free E-Book
Subscribe to:
Posts (Atom)
-
āļāļļ āļąොāļිāļēා 93i āļ¯ුāļģāļāļŽāļēāļ් āļ´ාāˇිāļ ්āļ ි āļāļģāļąāˇා āļąāļ¸්, āļāļŊිāļą් āļāļŊāļ§ āļāˇි āļ¸āļāļāļēāļ§ āļ āļ¯්āļˇූāļāļĸāļąāļ āļēāļ¸āļ් āˇිāļ¯ුāˇāļą āļļ⎠āļāļļ āļ āļ්āļ¯ැāļ āļිāļļෙāļąු āļāļි. āļිāˇිāļ¸ āˇේāļුāˇāļිāļą් āļොāļģ⎠āˇිāļ§ි āļ āļŠි...