想用winform写客户端,数据库用access,数据连接要放到什么地方,winform如何调用??谢谢!!

解决方案 »

  1.   

    在oleconnection控件的动态属性里选择connectionstring,添上连接字符串,会自动生成一个app.config
      

  2.   

    要是程序不大,直接写个ADO连接字符串在程序里
      

  3.   

    我现在用向导做的连接可以的,但是我象整个项目就一个连接,我新建的项目怎么没有app.config文件呢
      

  4.   

    手动写一个appName.config文件,可用的。
      

  5.   

    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Xml;
    namespace ACS
    {
    /// <summary>
    /// ACES 的摘要描述。
    /// 對數據進行基本的操作
    /// </summary>
    public class ACES
    {
    #region ACES 構造函數
    public ACES()
    {
    }
    #endregion #region 自定義變量
    //Link DataBase String
    private string sConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =ExTend.mdb ";

    #endregion #region Return a OleDbConnection 
    /// <summary>
    /// 返回一個數連接
    /// </summary>
    /// <returns>返回連接cn</returns>
    private OleDbConnection Acs_cn()
    {
    OleDbConnection cn = new OleDbConnection(sConString);
    if(cn.State == ConnectionState.Closed)
    {
    cn.Open();
    }
    return cn;
    }
    #endregion #region 查詢數據庫,返回查詢結果oDs
    /// <summary>
    /// 返回查詢結果
    /// </summary>
    /// <param name="sCmdString">查詢SQL字串</param>
    /// <returns></returns>
    public DataSet Retrieval(string sCmdString)
    {
    //建立連接
    OleDbConnection cn = Acs_cn();
    //建立Dataset對像
    DataSet oDs = new DataSet();
    try
    {
    //建立Adapter對像
    OleDbDataAdapter myAdapter = new OleDbDataAdapter(sCmdString,cn);
    myAdapter.Fill(oDs,"Result");
    }
    catch(Exception e)
    {
    throw e;
    }
    finally
    {
    cn.Close();
    }
    return oDs; }

    #endregion #region 與數據庫進行交易 
    /// <summary>
    /// 與數據庫進行交易 Succeed Return '1' Fail Return '-1'
    /// </summary>
    /// <param name="sCmdString">Operation SQL</param>
    /// <returns></returns>
    public int Translation(string sCmdString)
    {
    //定義操作變量,操作成功返回值為'1'
    int nResult = -1;
    //建立數據庫連接
    OleDbConnection cn = Acs_cn();
    //建立oledbcommand實例對像
    OleDbCommand myCommand = new OleDbCommand(sCmdString,cn);

    try
    {
    //執行操作
    myCommand.ExecuteNonQuery();
    nResult = 1;
    }
    catch(Exception e)
    {
    throw e;
    }
    finally
    {
    //關閉連接
    cn.Close();
    }
    return nResult; }
    #endregion } /// <summary>
    /// 對SQL語句中用的參數進行格式化
    /// </summary>
    public class FormatSql
    {
    #region 構造函數
    public FormatSql()
    {
    }
    #endregion #region Format SQL string
    public string SQLQ(string sText)
    {
    sText = "'"+sText+"'";
    return sText;
    } public string SQLQ(int n)
    {
    return n.ToString();
    } public string SQLQ(double n)
    {
    return n.ToString();
    } public string SQLQC(string sText)
    {
    sText = "'"+sText+"',";
    return sText;
    } public string SQLQC(int nText)
    {
    string sText = nText.ToString()+",";
    return sText;
    } public string SQLQC(double nText)
    {
    string sText = nText.ToString()+",";
    return sText;
    }
    #endregion
    }

    }
      

  6.   

    這是調用的!你自已寫SQL語句就可以了!
    using System;
    using System.Data;
    using ACS;namespace BarCode
    {
    /// <summary>
    /// Translation 的摘要描述。
    /// </summary>
    public class Translation
    {
    #region 構造函數
    public Translation()
    {
    }
    #endregion #region 基本變量 private string sql = "";
    private int nResult = -1;
    private ACS.ACES cmnExec = new ACS.ACES();
    private ACS.FormatSql cmnFormat = new FormatSql();
    private DataSet oDs = new DataSet();
    private DataSet oDs1 = new DataSet();
    private int nWip_Id = -1;
    public string sFactory_no = "";
    public string sWip_No = "";
    public string sPart_No = "";
    public string sBox_No_Start = "";
    public string sBox_Count = "";
    public string sEcn_Ver = "";
    public string sBios_Ver = "";
    public int nModel = -1;
    public string sLanguage = ""; #endregion #region 工單
    public int Wip()
    {
    //判斷數據庫中是否存在此筆記錄,不存在則插入,存在則找出
    //其對應的WIP_ID
    sql = " SELECT * FROM WIP_INFO WHERE WIP_NO="+cmnFormat.SQLQ(sWip_No);
    oDs = cmnExec.Retrieval(sql);
    if(oDs.Tables[0].Rows.Count>0)
    {
    nWip_Id = int.Parse(oDs.Tables[0].Rows[0]["WIP_ID"].ToString().Trim());
    nResult = nWip_Id;
    }
    else
    {
    sql = " INSERT INTO WIP_INFO";
    sql += "(FACTORY_NO,WIP_NO,PART_NO,ECN_VER,BIOS_VER,BOX_INCEPT";
    sql += ",BOX_SOURCE, CREATE_USER,CREATE_DATE,UPDATE_DATE)";
    sql += "VALUES("+cmnFormat.SQLQC(sFactory_no)+cmnFormat.SQLQC(sWip_No)+cmnFormat.SQLQC(sPart_No);
    sql += cmnFormat.SQLQC(sEcn_Ver)+cmnFormat.SQLQC(sBios_Ver);
    sql += cmnFormat.SQLQC(sBox_No_Start)+cmnFormat.SQLQC(sBox_Count)+cmnFormat.SQLQC("ADMINISTRATOR");
    sql += "Now(),Now())"; nResult = cmnExec.Translation(sql);
    //插入成功後找出其對應的WIP_ID
    if(nResult > 0)
    { sql = " SELECT * FROM WIP_INFO WHERE WIP_NO="+cmnFormat.SQLQ(sWip_No);
    oDs = cmnExec.Retrieval(sql);
    nWip_Id = int.Parse(oDs.Tables[0].Rows[0]["WIP_ID"].ToString().Trim());
    nResult = nWip_Id;
    }
                 }
    return nWip_Id;
    }
    #endregion
    }
    }
      

  7.   

    這是調用的!你自已寫SQL語句就可以了!
    using System;
    using System.Data;
    using ACS;namespace BarCode
    {
    /// <summary>
    /// Translation 的摘要描述。
    /// </summary>
    public class Translation
    {
    #region 構造函數
    public Translation()
    {
    }
    #endregion #region 基本變量 private string sql = "";
    private int nResult = -1;
    private ACS.ACES cmnExec = new ACS.ACES();
    private ACS.FormatSql cmnFormat = new FormatSql();
    private DataSet oDs = new DataSet();
    private DataSet oDs1 = new DataSet();
    private int nWip_Id = -1;
    public string sFactory_no = "";
    public string sWip_No = "";
    public string sPart_No = "";
    public string sBox_No_Start = "";
    public string sBox_Count = "";
    public string sEcn_Ver = "";
    public string sBios_Ver = "";
    public int nModel = -1;
    public string sLanguage = ""; #endregion #region 工單
    public int Wip()
    {
    //判斷數據庫中是否存在此筆記錄,不存在則插入,存在則找出
    //其對應的WIP_ID
    sql = " SELECT * FROM WIP_INFO WHERE WIP_NO="+cmnFormat.SQLQ(sWip_No);
    oDs = cmnExec.Retrieval(sql);
    if(oDs.Tables[0].Rows.Count>0)
    {
    nWip_Id = int.Parse(oDs.Tables[0].Rows[0]["WIP_ID"].ToString().Trim());
    nResult = nWip_Id;
    }
    else
    {
    sql = " INSERT INTO WIP_INFO";
    sql += "(FACTORY_NO,WIP_NO,PART_NO,ECN_VER,BIOS_VER,BOX_INCEPT";
    sql += ",BOX_SOURCE, CREATE_USER,CREATE_DATE,UPDATE_DATE)";
    sql += "VALUES("+cmnFormat.SQLQC(sFactory_no)+cmnFormat.SQLQC(sWip_No)+cmnFormat.SQLQC(sPart_No);
    sql += cmnFormat.SQLQC(sEcn_Ver)+cmnFormat.SQLQC(sBios_Ver);
    sql += cmnFormat.SQLQC(sBox_No_Start)+cmnFormat.SQLQC(sBox_Count)+cmnFormat.SQLQC("ADMINISTRATOR");
    sql += "Now(),Now())"; nResult = cmnExec.Translation(sql);
    //插入成功後找出其對應的WIP_ID
    if(nResult > 0)
    { sql = " SELECT * FROM WIP_INFO WHERE WIP_NO="+cmnFormat.SQLQ(sWip_No);
    oDs = cmnExec.Retrieval(sql);
    nWip_Id = int.Parse(oDs.Tables[0].Rows[0]["WIP_ID"].ToString().Trim());
    nResult = nWip_Id;
    }
                 }
    return nWip_Id;
    }
    #endregion
    }
    }