Quantcast
Channel: MySQL Forums - Connector/J, JDBC and Java
Viewing all articles
Browse latest Browse all 884

Connection to mysql database with .NET C# (1 reply)

$
0
0
Hi, I have tried for a long time now to get a connection to mysql database.
I am using Visual Studio 2008 with .NET and C#.
I have included my database on the server explorer and all apropriate tables are included but when I view the code in the browser it appears blank!
This is my connection code held in default.aspx.cs:

using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace trybuyconnect
{

public class Tutorial4
{
public static void Main()
{
string connStr = "server=localhost; id=root; database=database_name; password=******;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();

string sql = "SELECT * FROM product_list";
MySqlCommand cmd = new MySqlCommand(sql, conn);
object result = cmd.ExecuteScalar();
if (result != null)
{
int r = Convert.ToInt32(result);
Console.WriteLine("Products: " + r);
}

}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

conn.Close();
Console.WriteLine("Done.");
}
}
}

Does anyone know if there is a problem with the connection code.
Also, I havn't added any code to default.aspx, should I be adding code to that file?

Any suggestions?

Thanks

David

Viewing all articles
Browse latest Browse all 884

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>