Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Wednesday, September 3, 2008

Simple MySQL Database test

Sample code fragment:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using MySql.Data.MySqlClient;

public partial class Test_Page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Simple MySQL Database test


string MyConString = "Server=yourMySQLServer;Port=yourPort#;Database=YourDB;Uid=yourID;Pwd=yourPWD;";
MySqlConnection MyConnection = new MySqlConnection(MyConString);

string queryString = "SELECT * FROM YOUR_TABLE";

MySqlCommand MyCommand = new MySqlCommand();
MyCommand.CommandText = queryString;
MyCommand.Connection = MyConnection;

MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
dataAdapter.SelectCommand = MyCommand;
System.Data.DataSet dataSet = new System.Data.DataSet();
try
{
dataAdapter.Fill(dataSet);

GridView1.DataSource = dataSet;
GridView1.DataBind();

}
catch (Exception ex)
{
Label_Error.Text = ex.ToString();
}
}
}The sample above is a simple code fragment I used to test the MySQL connection. Very similar to the one before, but is setup to be a parameterized command eventually where I can add conditional statements like so to the queryString:

"... WHERE UserName = ?UserName"

and add the parameter like so:

MySqlParameter dbParam_UserName = new MySqlParameter();
dbParam_UserName.ParameterName = "?UserName";
dbParam_UserName.Value = UserName;
dbParam_UserName.DbType = System.Data.DbType.String;
MyCommand.Parameters.Add(dbParam_UserName);
I use parameterized commands instead of just imbeding the user input string into the SQL query string directly to help prevent SQL injection... at least I read somewhere it is supposed to help prevent that, combined with replacing any single quotes ' to double single quotes '', though I probably have a bit to learn about all the tricks to look out for.

More..
http://forums.asp.net/t/999456.aspx?PageIndex=2

An ASP.NET Application Using a MySQL Database

Abstract
.NET is the new distributed computing platform developed by Microsoft and ASP.NET is its programming model for web development. I'm creating a Web Form with a DataGrid for data entry using C#. How can I make a DataGrid cell display a textbox for showing a list of products in all the rows. This solution will teach you how to use a MySQL database with ASP.NET. I have developed this sample with ASP.NET 1.1 using C# as the code-behind using MySQL 4.1. I think this will be a more useful article for beginners studying .NET. In this sample, I have taken the DataGrid control for demonstration. All the basic operations of the DataGrid are explained in this article.

Overview of the Solution
MySQL:
MySQL Server 4.0 laid the foundation for the new features implemented in MySQL 4.1, such as subqueries and Unicode support, which were desired by many of our customers. The server is available as a separate program for use in a client/server networked environment. It is also available as a library that can be embedded (linked) into standalone applications. Such applications can be used in isolation or in environments where no network is available. Clients can connect to a MySQL server using TCP/IP sockets on any platform. On Windows systems belonging the NT family (NT, 2000, XP, or 2003), clients can connect using named pipes. On UNIX systems, clients can connect using UNIX domain socket files.

In MySQL 4.1, we cannot write stored procedures, functions, or views. The following are sample SELECT, UPDATE, and DELETE queries in MySQL.

SELECT column_names from table_name [WHERE ...conditions];

UPDATE table_name SET column_names = ‘’ WHERE ...conditions;

DELETE FROM table_name WHERE ...conditions;
ODBC:
The Microsoft Open Database Connectivity (ODBC) interface is a C programming language interface that makes it possible for applications to access data from a variety of database management systems. The ODBC interface permits maximum interoperability — an application can access data in diverse DBMSs through a single interface. Furthermore, that application will be independent of any DBMS from which it accesses data. Users of the application can add software components called drivers, which interface between an application and a specific DBMS. Applications that use ODBC are responsible for any cross-database functionality. For example, ODBC is not a heterogeneous join engine, nor is it a distributed transaction processor. However, because it is DBMS-independent, it can be used to build such cross-database tools.

More...
http://www.codeproject.com/KB/database/mysqlinaspnet.aspx

Sunday, August 31, 2008

An ASP.NET Application Using a MySQL Database

An ASP.NET Application Using a MySQL Database
By vivekthangaswamy

How to use a MySQL 4.1 database from an ASP.NET application and some ODBC basics.

Abstract
.NET is the new distributed computing platform developed by Microsoft and ASP.NET is its programming model for web development. I'm creating a Web Form with a DataGrid for data entry using C#. How can I make a DataGrid cell display a textbox for showing a list of products in all the rows. This solution will teach you how to use a MySQL database with ASP.NET. I have developed this sample with ASP.NET 1.1 using C# as the code-behind using MySQL 4.1. I think this will be a more useful article for beginners studying .NET. In this sample, I have taken the DataGrid control for demonstration. All the basic operations of the DataGrid are explained in this article.

Overview of the Solution
MySQL:
MySQL Server 4.0 laid the foundation for the new features implemented in MySQL 4.1, such as subqueries and Unicode support, which were desired by many of our customers. The server is available as a separate program for use in a client/server networked environment. It is also available as a library that can be embedded (linked) into standalone applications. Such applications can be used in isolation or in environments where no network is available. Clients can connect to a MySQL server using TCP/IP sockets on any platform. On Windows systems belonging the NT family (NT, 2000, XP, or 2003), clients can connect using named pipes. On UNIX systems, clients can connect using UNIX domain socket files.

In MySQL 4.1, we cannot write stored procedures, functions, or views. The following are sample SELECT, UPDATE, and DELETE queries in MySQL.

SELECT column_names from table_name [WHERE ...conditions];

UPDATE table_name SET column_names = ‘’ WHERE ...conditions;

DELETE FROM table_name WHERE ...conditions;
ODBC:
The Microsoft Open Database Connectivity (ODBC) interface is a C programming language interface that makes it possible for applications to access data from a variety of database management systems. The ODBC interface permits maximum interoperability — an application can access data in diverse DBMSs through a single interface. Furthermore, that application will be independent of any DBMS from which it accesses data. Users of the application can add software components called drivers, which interface between an application and a specific DBMS. Applications that use ODBC are responsible for any cross-database functionality. For example, ODBC is not a heterogeneous join engine, nor is it a distributed transaction processor. However, because it is DBMS-independent, it can be used to build such cross-database tools.

The following will explain to you the architecture of ODBC:


Namespace used for ODBC:

using System.Data.Odbc;
The System.Data.Odbc namespace is the .NET Framework Data Provider for ODBC.

The .NET Framework Data Provider for ODBC describes a collection of classes used to access an ODBC data source in the managed space. Using the OdbcDataAdapter class, you can fill a memory-resident DataSet, which you can use to query and update a data source.

http://www.codeproject.com/KB/database/mysqlinaspnet.aspx

ASP.Net Feeds