Eftersom mitt Access har flippat ur, har jag försökt byta DBMS till MySQL. Jag ska göra en enkel gästbok och får med nuvarande kod felet:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.Data.Odbc.OdbcException: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Source Error:
Line 15: GBentries objGBentries = new GBentries(MyConString);
Line 16:
Line 17: DataGrid1.DataSource = objGBentries.getGBentries();
Line 18: DataGrid1.DataBind();
Line 19: }
Så här ser code-behinden till default.aspx ut:
//default_cb.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using guestbook;
public class DefaultCodeBehind : Page
{
public DataGrid DataGrid1;
public void Page_Load(object source, EventArgs e)
{
try
{
string MyConString = "DSN=myODBC";
GBentries objGBentries = new GBentries(MyConString);
DataGrid1.DataSource = objGBentries.getGBentries();
DataGrid1.DataBind();
}
catch (ArgumentException f)
{
Response.Write(f.Message);
}
}
}
Och så här ser koden till min komponent som ska hämta inlägg i gästbokstabellen ut:
//getGBentries.cs
using System;
using System.Data;
using Microsoft.Data.Odbc;
//using System.Data.OleDb;
namespace guestbook
{
public class GBentries
{
private string m_DSN;
public GBentries(string MyConString)
{
m_DSN = MyConString;
}
public DataSet getGBentries()
{
OdbcConnection myConnection = new OdbcConnection(m_DSN);
myConnection.Open();
OdbcDataAdapter myDataAdapter = new OdbcDataAdapter("select * from gbook", myConnection);
DataSet GBentries = new DataSet();
myDataAdapter.Fill(GBentries);
return GBentries;
}
}
}
Om det är något annat ni vill veta så säg till! :)
1. Click on the start menu -> Settings -> Control Panel -> Administrative Tools -> Data Sources (ODBC) menu option. This will load the ODBC data source administrator tab.
2. Because we want to create a system DSN that is accessible to any user logged into our web server, click on the "System DSN" tab, and then on the "Add..." button.
3. Select the MySQL driver from the bottom of the list and click the finish button.
4. The MySQL driver configuration dialog appears. It contains seven text boxes and a variety of check boxes.
Into the Windows DSN name field, enter "mysql_dsn". This is simply the name that we will use to refer to our DSN from within ASP. Into the MySQL host field, enter either the Net BIOS name or I.P. address of your MySQL server. If you have MySQL installed locally (on the same machine that you are creating the system DSN on), then enter 127.0.0.1.
In the database name field, enter the name of our address book database, "address". This tells MySQL the name of the database we want to issue our SQL queries to when we are connected.
Enter your MySQL username and password details into the user and password fields. If you aren't sure what they are and you're running MySQL on Windows, then run c:\mysql\bin\winmysql.exe and click on the "my.ini Setup" tab. Under the "[WinMySQLAdmin]" section, you'll find your default MySQL username and password. Leave both the port and sql command on connect fields empty.
5. Lastly, make sure that the "Return matching rows" checkbox is ticked. Click on the OK button to create your new MySQL system DSN.
Jo det kan du iofs ha rätt i (har stirrat mig blind ett bra tag på själva koden ett tag ;) ). Jag vet inte riktigt hur MySQL fungerar jämfört med Access. Måste jag kanske köra igång någon process först, för att kunna använda databasen? :)
Jo, den är installerad. Jag har hela tiden följt hänvisningarna från mysql.com men ändå fungerar det inte. Kanske beror på någon inställning på min dator?
Ähum... tydligen kunde jag kommma åt datan om jag skippade lösenordet... även om jag ställt in ett lösenord i dns:en. Lite underligt...
Kan det bero på att det är jag som är localhost och att den då inte bryr sig om lösenordet?
Sedan har jag två andra frågor också.
1. Varför får inte min "felmeddelandevariabel heta "e" i denna koden:
catch (ArgumentException f)
{
Response.Write(f.Message);
}
Om jag döper den till e får jag:
A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else
även om jag varken deklarerat en sådan variabel i code-behinden eller i komponenten.
2. Är det någon som fått "ErrorObject.HelpLink" att fungera? När jag försöker visas ingenting...´
Tack alla som hjhälpt till hitintills.
edit) 3. Tydligen visas alla kolummer som är av typen varchar(30) och int men inte den som är av typen "blob". Hur kan det komma sig? :q