Linq到MySQL的运营 [英] Linq to MySQL operations
问题描述
Hello Friends ...
我正在使用DbLinq连接asp.net应用程序中的MySQL数据库。
我找到了很好的DbMetal.exe来生成.cs文件和.dbml文件以与MySQL一起工作,这需要同一目录中的一些dll。
以下代码用于获取数据
Hello Friends...
I'm using DbLinq to connect with MySQL database in asp.net application.
Well I found good DbMetal.exe that generate the .cs file and .dbml file to work with MySQL which required some of the dll in the same directory.
following code I used to fetch data
DBLINQ.ManIsHTest db = new DBLINQ.ManIsHTest(new MySqlConnection("Database=test_db;Data Source=localhost;User Id=root;Password=abc"));
var mtest = from m in db.McAt select m;
GridView1.DataSource = mtest;
GridView1.DataBind();
它工作得非常好..没有问题这个
现在我手动制作我的.cs文件,其中包含映射到数据库中表和列的类,与使用DbMetal.exe的自动生成文件相同br />
文件成功创建...没问题这个
我拿的表包含更多列,但我需要的很少。
我使用了传递连接字符串它很好但它在 Gridview1.Databind(); 给出错误检查synatx查询的错误。
然后我认为它无法理解它是MySQL查询还是SQL Server。因此最好放置提供者名称然后我不知道如何给它MappningSource。
但是当我使用它时会给出一个错误MappingSource。请告诉我如何通过映射源。
请指导我或告诉我你是否知道其他替代方法。
And It was working absolutely fine..No issue with this
Now I made manually my .cs file that contain the classes mapping to the tables and columns in the database same as auto generated file using DbMetal.exe
file created successfully... No issue with this
The table, I am fetching, contains more columns but I need few.
I used pass the connectionstring it was fine but it gives an error on Gridview1.Databind(); gives an error that "Check the synatx to query".
Then I thought it wouldn't be able to understand whether it is MySQL query or SQL Server. So it's better to put provider name then I'm not getting how to give it MappningSource.
But when I use this gives an error MappingSource. Please tell me how can i pass the mapping source.
Please guide me or tell me if you know other alternate way to do this.
namespace DBLINQ
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
/// <summary>
/// Summary description for Class1
/// </summary>
using DbLinq;
using DbLinq.Data.Linq;
using DbLinq.Data.Linq.Mapping;
using DbLinq.Vendor;
using DbLinq.Vendor.Implementation;
public partial class Class1 : DbLinq.Data.Linq.DataContext
{
#region Extensiblity Method Declartion
partial void OnCreated();
#endregion
public Class1(string connectionString)
: base(connectionString)
{
this.OnCreated();
}
public Class1(string connection, MappingSource mappingSource)
: base(connection, mappingSource)
{
this.OnCreated();
}
public Class1(IDbConnection connection, MappingSource mappingSource)
: base(connection, mappingSource)
{
this.OnCreated();
}
//public Class1(IDbConnection connection)
// : base(connection)
//{
// this.OnCreated();
//}
public Table<Prod> Prod
{
get
{
return this.GetTable<Prod>();
}
}
public Table<McAt> McAt
{
get
{
return this.GetTable<McAt>();
}
}
}
[Table(Name = "manish_test.prod")]
public partial class Prod
{
private int _id;
private string _productID;
private string _brand;
private string _productName;
private System.Nullable<float> _listPrice;
private System.Nullable<float> _mrp;
private System.Nullable<float> _discount;
#region Extensibility methods
partial void OnCreated();
partial void OnIDChanged();
partial void OnIDChanging(int value);
partial void OnProductIDChanged();
partial void OnProductIDChanging(string value);
partial void OnBrandChanged();
partial void OnBrandChanging(string value);
partial void OnProductNameChanged();
partial void OnProductNameChanging(string value);
partial void OnListPriceChanged();
partial void OnListPriceChanging(System.Nullable<float> value);
partial void OnMrpChanged();
partial void OnMrpChanging(System.Nullable<float> value);
partial void OnDiscountChanged();
partial void OnDiscountChanging(System.Nullable<float> value);
#endregion
public Prod()
{
this.OnCreated();
}
#region Declaring properties that represent colums in the database
[Column(Name = "ID", Storage = "_id", DbType = "int", CanBeNull = false, AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public int ID
{
get
{
return this._id;
}
set
{
if (_id != value)
{
this.OnIDChanging(value);
this._id = value;
this.OnIDChanged();
}
}
}
[Column(Name = "ProductID", Storage = "_productID", AutoSync = AutoSync.Never, DbType = "varchar(30)", CanBeNull = false)]
[DebuggerNonUserCode()]
public string ProductID
{
get { return _productID; }
set
{
if ((_productID != value))
{
this.OnProductIDChanging(value);
this._productID = value;
this.OnProductIDChanged();
}
}
}
[Column(Name = "Brand", Storage = "_brand", DbType = "varchar(60)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public string Brand
{
get { return _brand; }
set
{
if ((_brand != value))
{
this.OnBrandChanging(value);
this._brand = value;
this.OnBrandChanged();
}
}
}
[Column(Name = "ProductName", Storage = "_productName", DbType = "varchar(200)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public string ProductName
{
get { return _productName; }
set
{
if ((_brand != value))
{
this.OnProductNameChanging(value);
this._productName = value;
this.OnBrandChanged();
}
}
}
[Column(Name = "ListPrice", Storage = "_listPrice", DbType = "float(10,2)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public Nullable<float> ListPrice
{
get { return _listPrice; }
set
{
if ((_listPrice != value))
{
this.OnListPriceChanging(value);
this._listPrice = value;
this.OnListPriceChanged();
}
}
}
[Column(Name = "MRP", Storage = "_mrp", DbType = "float(10,2)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public System.Nullable<float> MRP
{
get { return _mrp; }
set
{
if ((_mrp != value))
{
this.OnMrpChanging(value);
this._mrp = value;
this.OnMrpChanged();
}
}
}
[Column(Name = "Discount", Storage = "_discount", DbType = "float(3,2)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public System.Nullable<float> Discount
{
get { return _discount; }
set
{
if ((_discount != value))
{
this.OnDiscountChanging(value);
this._discount = value;
this.OnDiscountChanged();
}
}
}
#endregion
}
}
protected void btn_Click(object sender, EventArgs e)
{
System.Data.Linq.Mapping.ProviderAttribute ab = new ProviderAttribute(typeof(MySql.Data.MySqlClient.MySqlDriverType));
DBLINQ.Class1 db = new DBLINQ.Class1(new MySqlConnection("Database=test_db;Data Source=localhost;User Id=root;Password=abc"),ab);
var myData = from p in db.McAt select p;
GridView1.DataSource = myData;
GridView1.DataBind();
}
谢谢和问候
Thanks and Regards
推荐答案
Hi Manish ,试试这个:
Hi Manish, try this:
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Linq;
using MySql.Data.MySqlClient;
using DbLinq.Data.Linq;
namespace DBLINQ
{
public partial class Class1 : DbLinq.MySql.MySqlDataContext // <---- LOOK THIS!!
{
#region Extensiblity Method Declartion
partial void OnCreated();
#endregion
/********************************************************************
*
*
public Class1(string connectionString)
: base(connectionString)
{
this.OnCreated();
}
public Class1(string connection, MappingSource mappingSource)
: base(connection, mappingSource)
{
this.OnCreated();
}
public Class1(IDbConnection connection, MappingSource mappingSource)
: base(connection, mappingSource)
{
this.OnCreated();
}
*
*
*********************************************************************/
public Class1(IDbConnection connection) // <-- UNCOMMENT!
: base(connection)
{
this.OnCreated();
}
public Table<prod> Prod
{
get
{
return this.GetTable<prod>();
}
}
public Table<mcat> McAt
{
get
{
return this.GetTable<mcat>();
}
}
}
[Table(Name = "manish_test.prod")]
public partial class Prod
{
private int _id;
private string _productID;
private string _brand;
private string _productName;
private System.Nullable<float> _listPrice;
private System.Nullable<float> _mrp;
private System.Nullable<float> _discount;
#region Extensibility methods
partial void OnCreated();
partial void OnIDChanged();
partial void OnIDChanging(int value);
partial void OnProductIDChanged();
partial void OnProductIDChanging(string value);
partial void OnBrandChanged();
partial void OnBrandChanging(string value);
partial void OnProductNameChanged();
partial void OnProductNameChanging(string value);
partial void OnListPriceChanged();
partial void OnListPriceChanging(System.Nullable<float> value);
partial void OnMrpChanged();
partial void OnMrpChanging(System.Nullable<float> value);
partial void OnDiscountChanged();
partial void OnDiscountChanging(System.Nullable<float> value);
#endregion
public Prod()
{
this.OnCreated();
}
#region Declaring properties that represent colums in the database
[Column(Name = "ID", Storage = "_id", DbType = "int", CanBeNull = false, AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public int ID
{
get
{
return this._id;
}
set
{
if (_id != value)
{
this.OnIDChanging(value);
this._id = value;
this.OnIDChanged();
}
}
}
[Column(Name = "ProductID", Storage = "_productID", AutoSync = AutoSync.Never, DbType = "varchar(30)", CanBeNull = false)]
[DebuggerNonUserCode()]
public string ProductID
{
get { return _productID; }
set
{
if ((_productID != value))
{
this.OnProductIDChanging(value);
this._productID = value;
this.OnProductIDChanged();
}
}
}
[Column(Name = "Brand", Storage = "_brand", DbType = "varchar(60)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public string Brand
{
get { return _brand; }
set
{
if ((_brand != value))
{
this.OnBrandChanging(value);
this._brand = value;
this.OnBrandChanged();
}
}
}
[Column(Name = "ProductName", Storage = "_productName", DbType = "varchar(200)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public string ProductName
{
get { return _productName; }
set
{
if ((_brand != value))
{
this.OnProductNameChanging(value);
this._productName = value;
this.OnBrandChanged();
}
}
}
[Column(Name = "ListPrice", Storage = "_listPrice", DbType = "float(10,2)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public Nullable<float> ListPrice
{
get { return _listPrice; }
set
{
if ((_listPrice != value))
{
this.OnListPriceChanging(value);
this._listPrice = value;
this.OnListPriceChanged();
}
}
}
[Column(Name = "MRP", Storage = "_mrp", DbType = "float(10,2)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public System.Nullable<float> MRP
{
get { return _mrp; }
set
{
if ((_mrp != value))
{
this.OnMrpChanging(value);
this._mrp = value;
this.OnMrpChanged();
}
}
}
[Column(Name = "Discount", Storage = "_discount", DbType = "float(3,2)", AutoSync = AutoSync.Never)]
[DebuggerNonUserCode()]
public System.Nullable<float> Discount
{
get { return _discount; }
set
{
if ((_discount != value))
{
this.OnDiscountChanging(value);
this._discount = value;
this.OnDiscountChanged();
}
}
}
#endregion
}
}
using System;
using System.Data;
using System.Linq;
using MySql.Data.MySqlClient;
using DbLinq.Data.Linq;
protected void btn_Click(object sender, EventArgs e)
{
string connectstring = @"Database=[mydatabase];
Server=[myserver];
User Id=[myuserid];
Password=[mypassword];
Pooling=false;";
IDbConnection dbcon = new MySqlConnection(connectstring);
Class1 dc = new Class1(dbcon);
var products = from c in dc.Prod select c;
foreach (Prod t in products)
Response.Write(t.Brand + "," + t.ProductName);
// GridView1.DataSource = Class1.Prod;
GridView1.DataSource = dc.Prod; // <-- THIS IS CORRECT!!!
GridView1.DataBind();
}
祝你好运,
JL
来自其他JL答案的protected void btn_Click中的更正 - Nelek
GridView1.DataSource = (DBLINQ.Class1)myData;
GridView1.DataBind();
< b>< i>< i>< i>< i>< U>< U>< U>< / U>< / U>< / U>< / I>< / I>< / I>< / I>< / b个 [ ]
<b><i><i><i><i><u><u><u></u></u></u></i></i></i></i></b>[]
这篇关于Linq到MySQL的运营的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!