读书人

linq to sqlite 查询顺利更新和插入

发布时间: 2013-07-04 11:45:28 作者: rapoo

linq to sqlite 查询成功,更新和插入失败?附上源码,请帮忙调试
本帖最后由 q107770540 于 2013-06-29 21:34:23 编辑

// 
// ____ _ __ __ _ _
// | _ \| |__ | \/ | ___| |_ __ _| |
// | | | | '_ \| |\/| |/ _ \ __/ _` | |
// | |_| | |_) | | | | __/ || (_| | |
// |____/|_.__/|_| |_|\___|\__\__,_|_|
//
// Auto-generated from main on 2013-06-29 10:07:33Z.
// Please visit http://code.google.com/p/dblinq2007/ for more information.
//
namespace Demo
{
using System;
using System.ComponentModel;
using System.Data;

using DbLinq.Data.Linq;
using DbLinq.Vendor;

using System.Data.Linq.Mapping;
using System.Diagnostics;


public partial class main : DataContext
{

#region Extensibility Method Declarations
partial void OnCreated();
#endregion


public main(string connectionString) :
base(connectionString)
{
this.OnCreated();
}

public main(string connection, MappingSource mappingSource) :
base(connection, mappingSource)
{
this.OnCreated();
}

public main(IDbConnection connection, MappingSource mappingSource) :
base(connection, mappingSource)
{
this.OnCreated();
}

public Table<Product> Product
{
get
{
return this.GetTable<Product>();
}
}
}

#region Start MONO_STRICT

public partial class main
{

public main(IDbConnection connection) :
base(connection)
{
this.OnCreated();
}
}

#endregion


#region End Not MONO_STRICT
#endregion
// MONO_STRICT

[Table(Name = "main.Product" ) ]
public partial class Product
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

private string _prodCode;



private string _prodName;

#region Extensibility Method Declarations
partial void OnCreated();

partial void OnProdCodeChanged();

partial void OnProdCodeChanging(string value);

partial void OnProdNameChanged();

partial void OnProdNameChanging(string value);
#endregion


public Product()
{
this.OnCreated();
}

[Column(Storage = "_prodCode", Name = "ProdCode", DbType = "CHAR(12)", AutoSync = AutoSync.Never, CanBeNull = false, IsPrimaryKey = true)]
[DebuggerNonUserCode()]
public string ProdCode
{
get
{
return this._prodCode;
}
set
{
if (((_prodCode == value)
== false))
{
this.OnProdCodeChanging(value);
this._prodCode = value;
this.OnProdCodeChanged();
}
}
}

[Column(Storage="_prodName", Name="ProdName", DbType="CHAR(40)", AutoSync=AutoSync.Never)]
[DebuggerNonUserCode()]
public string ProdName
{
get
{
return this._prodName;
}
set
{
if (((_prodName == value)
== false))
{
this.OnProdNameChanging(value);
this._prodName = value;
this.OnProdNameChanged();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Linq;


namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//不适用Linq
string constr = string.Format("Data Source =\"{0}\";", @"myServer.db");


SQLiteConnection con = new SQLiteConnection(constr);
con.Open();
SQLiteCommand com = new SQLiteCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "Update Product set ProdName='llf' where ProdCode='001'";
com.ExecuteNonQuery();
}
public static IDbConnection GetConnection()
{

var filename = string.Format("myServer.db");
return new System.Data.SQLite.SQLiteConnection("data source=" + filename) as IDbConnection;
}
private void button2_Click(object sender, EventArgs e)
{
//读取Linq
main comn = new main(Form1.GetConnection());
var Product1 = from o in comn.Product
select o;

foreach (var item in Product1)
{
MessageBox.Show( string.Join("/", new string[] { item.ProdCode.ToString(), item.ProdName.ToString() }) + "\n");
}



MessageBox.Show("ok!");
}

private void button3_Click(object sender, EventArgs e)
{
//写入Linq
Product product1 = new Product();

main comn = new main(Form1.GetConnection());

product1.ProdCode = "0002";

product1.ProdName = "00001";
comn.Product.InsertOnSubmit(product1);
comn.SubmitChanges();
}

private void button4_Click(object sender, EventArgs e)
{
//修改Linq
main comn = new main(Form1.GetConnection());
Product product = comn.Product.Single(c => c.ProdCode == "001");
if (product != null)
{
product.ProdName = "测试更改";
comn.SubmitChanges();
}

}
}
}

源代码


[解决办法]
Product 表,没有主键?
[解决办法]
你最好把Product的结构贴出来
[解决办法]
这文件头部的注释 真闪眼哈 linq to sqlite 查询顺利,更新和插入失败?附上源码,请帮忙调试
没想到都有 linq to sqlite 了。。。。

读书人网 >.NET

热点推荐