29-12-2007, 21:36
|
|
|
חבר מתאריך: 09.12.06
הודעות: 2
|
|
צירפתי כאן את המחלקה בה כתבתי את הקוד שנקראת DBMANAGER
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
namespace Server
{
class DBManager
{
OdbcConnection m_dbConnecion;
public DBManager()
{
m_dbConnecion = new OdbcConnection(@"Dsn=MS Access Database;dbq=C:\Documents and Settings\User\Desktop\Chetoz\IMDatabase.mdb;defaul tdir=C:\Documents and Settings\User\Desktop\Chetoz;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5");
}
#region Read
public ServerUser GetUserById(int id)
{
ServerUser user = null;
OdbcCommand cmd = new OdbcCommand("SELECT * FROM Clients Where Clients.id = @index", m_dbConnecion);
cmd.Parameters.AddWithValue("@index", id);
m_dbConnecion.Open();
try
{
OdbcDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.Clos eConnection);
if (reader.Read())
{
user = new ServerUser((int)reader["id"], (string)reader["UserName"], (string)reader["Password"], (bool)reader["IsConnected"]);
}
}
finally
{
m_dbConnecion.Close();
}
return user;
}
#endregion
#region Write
public void InsertUser(string userName, string password, bool isConnected)
{
OdbcCommand cmd = new OdbcCommand("INSERT INTO Clients (UserName,Password,IsConnected) VALUES (@user,@pass,@isCon)",m_dbConnecion);
cmd.Parameters.AddWithValue("@user", userName);
cmd.Parameters.AddWithValue("@pass", password);
cmd.Parameters.AddWithValue("@isCon", isConnected);
m_dbConnecion.Open();
cmd.ExecuteNonQuery();
m_dbConnecion.Close();
}
#endregion
}
}
|