2个回答
2013-07-17
展开全部
这个例子是我初学时做的一个项目。你可以看看。using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Configuration;
using XinXingModels;namespace XinXingDAL
{
public class ProgramService
{
#region Private Members
//从配置文件中读取数据库连接字符串
private readonly string connString =
ConfigurationManager.ConnectionStrings["XinXingConnectionString"].ToString();
private readonly string dboOwner =
ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();
#endregion /// <summary>
/// 查询所有账户信息
/// </summary>
/// <returns></returns>
public IList<Account> SearchAllRemarks()
{
List<Account> account = new List<Account>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand objCommand = new OleDbCommand("select * from Account", conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
Account a = new Account();
a.Id = Convert.ToInt32(objReader["ID"]);
a.Remarks = Convert.ToString(objReader["Remarks"]);
a.LoginID = Convert.ToString(objReader["LoginID"]);
a.Pwd = Convert.ToString(objReader["Pwd"]);
a.TwoPwd = Convert.ToString(objReader["TwoPwd"]);
a.Type = Convert.ToString(objReader["Type"]);
account.Add(a);
}
objReader.Dispose(); conn.Close();
conn.Dispose();
}
return account;
}
/// <summary>
/// 根据ID和备注查询相关信息
/// </summary>
/// <param name="id"></param>
/// <param name="remarks"></param>
/// <returns></returns>
public IList<Account> SelectInfoByRemarksAndID(int id, string remarks)
{
List<Account> account = new List<Account>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
Account a = new Account();
string sql = string.Format("select LoginID,Pwd,TwoPwd,Type from Account Where Id={0} and remarks='{1}'", id, remarks);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
if (objReader.Read())
{
a.LoginID = Convert.ToString(objReader["LoginID"]);
a.Pwd = Convert.ToString(objReader["Pwd"]);
a.Type = Convert.ToString(objReader["Type"]);
a.TwoPwd = Convert.ToString(objReader["TwoPwd"]);
account.Add(a);
}
}
return account;
}
/// <summary>
/// 查询所有备忘录
/// </summary>
/// <returns></returns>
public IList<Notepad> SearchAllInfoFromNotepad()
{
List<Notepad> notepad = new List<Notepad>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand objCommand = new OleDbCommand("select * from Notepad", conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
Notepad n = new Notepad();
n.Id = Convert.ToInt32(objReader["ID"]);
n.Time = Convert.ToDateTime(objReader["Times"]);
n.Content = Convert.ToString(objReader["Content"]);
notepad.Add(n);
}
objReader.Dispose();
conn.Close();
conn.Dispose();
}
return notepad;
}
/// <summary>
/// 将备忘录插入
/// </summary>
/// <returns></returns>
public int InsertAllInfoIntoNotepad(string note)
{
int id;
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("insert into Notepad(content) values('{0}')", note);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
id = objCommand.ExecuteNonQuery();
}
return id;
}
/// <summary>
/// 查询最后一条记录
/// </summary>
/// <returns></returns>
public string SelectLastNotepad()
{
string message = "\n";
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("SELECT top 1 * from notepad order by ID desc");
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
message += Convert.ToDateTime(objReader["Times"]) + "\n";
message += Convert.ToString(objReader["Content"]);
}
}
return message;
} public int InsertAccount(string loginID, string pwd,string twoPwd,string remarks,string type)
{
int id;
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("insert into Account(LoginID,Pwd,TwoPwd,Remarks,Type) values('{0}','{1}','{2}','{3}','{4}')", loginID, pwd, twoPwd, remarks, type);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
id = objCommand.ExecuteNonQuery();
}
return id;
}
}}
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Configuration;
using XinXingModels;namespace XinXingDAL
{
public class ProgramService
{
#region Private Members
//从配置文件中读取数据库连接字符串
private readonly string connString =
ConfigurationManager.ConnectionStrings["XinXingConnectionString"].ToString();
private readonly string dboOwner =
ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();
#endregion /// <summary>
/// 查询所有账户信息
/// </summary>
/// <returns></returns>
public IList<Account> SearchAllRemarks()
{
List<Account> account = new List<Account>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand objCommand = new OleDbCommand("select * from Account", conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
Account a = new Account();
a.Id = Convert.ToInt32(objReader["ID"]);
a.Remarks = Convert.ToString(objReader["Remarks"]);
a.LoginID = Convert.ToString(objReader["LoginID"]);
a.Pwd = Convert.ToString(objReader["Pwd"]);
a.TwoPwd = Convert.ToString(objReader["TwoPwd"]);
a.Type = Convert.ToString(objReader["Type"]);
account.Add(a);
}
objReader.Dispose(); conn.Close();
conn.Dispose();
}
return account;
}
/// <summary>
/// 根据ID和备注查询相关信息
/// </summary>
/// <param name="id"></param>
/// <param name="remarks"></param>
/// <returns></returns>
public IList<Account> SelectInfoByRemarksAndID(int id, string remarks)
{
List<Account> account = new List<Account>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
Account a = new Account();
string sql = string.Format("select LoginID,Pwd,TwoPwd,Type from Account Where Id={0} and remarks='{1}'", id, remarks);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
if (objReader.Read())
{
a.LoginID = Convert.ToString(objReader["LoginID"]);
a.Pwd = Convert.ToString(objReader["Pwd"]);
a.Type = Convert.ToString(objReader["Type"]);
a.TwoPwd = Convert.ToString(objReader["TwoPwd"]);
account.Add(a);
}
}
return account;
}
/// <summary>
/// 查询所有备忘录
/// </summary>
/// <returns></returns>
public IList<Notepad> SearchAllInfoFromNotepad()
{
List<Notepad> notepad = new List<Notepad>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand objCommand = new OleDbCommand("select * from Notepad", conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
Notepad n = new Notepad();
n.Id = Convert.ToInt32(objReader["ID"]);
n.Time = Convert.ToDateTime(objReader["Times"]);
n.Content = Convert.ToString(objReader["Content"]);
notepad.Add(n);
}
objReader.Dispose();
conn.Close();
conn.Dispose();
}
return notepad;
}
/// <summary>
/// 将备忘录插入
/// </summary>
/// <returns></returns>
public int InsertAllInfoIntoNotepad(string note)
{
int id;
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("insert into Notepad(content) values('{0}')", note);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
id = objCommand.ExecuteNonQuery();
}
return id;
}
/// <summary>
/// 查询最后一条记录
/// </summary>
/// <returns></returns>
public string SelectLastNotepad()
{
string message = "\n";
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("SELECT top 1 * from notepad order by ID desc");
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
message += Convert.ToDateTime(objReader["Times"]) + "\n";
message += Convert.ToString(objReader["Content"]);
}
}
return message;
} public int InsertAccount(string loginID, string pwd,string twoPwd,string remarks,string type)
{
int id;
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("insert into Account(LoginID,Pwd,TwoPwd,Remarks,Type) values('{0}','{1}','{2}','{3}','{4}')", loginID, pwd, twoPwd, remarks, type);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
id = objCommand.ExecuteNonQuery();
}
return id;
}
}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-17
展开全部
分两部分:1.链接部分:你使用的语言与数据库的链接函数的查询,
2.数据操作过程:对应函数执行SQL语句,然后使用结果获取函数得到结果,然后把结果赋值给你的变量。
2.数据操作过程:对应函数执行SQL语句,然后使用结果获取函数得到结果,然后把结果赋值给你的变量。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询