ASP.NET Tutorial/ADO.net Database/OdbcDataAdapter
Версия от 15:30, 26 мая 2010; (обсуждение)
Use OdbcDataAdapter to fill a DataSet
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MySQLConnectString"]);
string CommandText = "SELECT * FROM Publisher";
OdbcConnection myConnection = new OdbcConnection(ConnectionString);
OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
OdbcDataAdapter myAdapter = new OdbcDataAdapter();
myAdapter.SelectCommand = myCommand;
DataSet myDataSet = new DataSet();
try {
myConnection.Open();
myAdapter.Fill(myDataSet);
} catch (Exception ex) {
throw (ex);
} finally {
myConnection.Close();
}
myGrid.DataSource = myDataSet;
myGrid.DataBind();
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DataGrid id="myGrid" runat="server"></asp:DataGrid>
</form>
</body>
</html>
File: Web.config
<configuration>
<appSettings>
<add key="MySQLConnectString"
value="driver={MySQL ODBC 3.51 Driver};server=localhost;database=Books;uid=YourID;pwd=letmein;" />
</appSettings>
</configuration>