1. 程式人生 > >ADO.NET之儲存圖片路徑

ADO.NET之儲存圖片路徑

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace UseFilePath
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        SqlConnection con = new SqlConnection(//定義資料庫連線欄位
@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DataBase\db_TomeTwo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        string strPath = "";//定義檔案路徑欄位

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            string[] strSex = { "男", "女" };//建立字串陣列
            this.comboBox1.DataSource = strSex;//設定資料來源
            string[] strCall =//建立字串陣列
            { "員工", "主幹人員", "部門經理", "經理" };
            this.comboBox2.DataSource = strCall;//設定資料來源
            string[] strHunYin =//建立字串陣列
            { "以婚", "未婚", "離異" };
            this.comboBox3.DataSource = strHunYin;//設定資料來源
            string[] strJianKang =//建立字串陣列
            { "很好", "良好", "一般" };
            this.comboBox4.DataSource = strJianKang;//設定資料來源
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            this.openFileImage.ShowDialog();//顯示圖片並得到路徑
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (TextInfo())
            {
                if (insertInfo())
                {
                    MessageBox.Show("新增成功");//彈出訊息對話方塊
                    clearText();//清空TextBox控制元件文字內容
                }
            }
            else
            {
                MessageBox.Show("請輸入正確資訊");//彈出訊息對話方塊
            }
        }

        private void clearText()
        {
            foreach (Control c in groupBox1.Controls)
            {
                if (c is TextBox)
                {
                    c.Text = "";//清空控制元件文字內容
                }
            }
            pictureBox1.Image = null;//取消顯示圖片
        }

        private Boolean TextInfo()
        {
            foreach (Control c in groupBox1.Controls)
            {
                if (c is TextBox)
                {
                    if (c.Text == "")//判斷內容是否為空
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        private void openFileImage_FileOk(object sender, CancelEventArgs e)
        {
            strPath = this.openFileImage.FileName;//得到影象檔案路徑
            this.pictureBox1.Image = Image.FromFile(strPath);//顯示影象
        }

        private bool insertInfo()
        {
            try
            {
                con.Open();//開啟資料庫連線
                StringBuilder strSql = new StringBuilder();//建立StringBuilder物件
                strSql.Append("insert into 檔案資訊 values(@檔案編號,@工號,");//追加文字內容
                strSql.Append("@姓名,@照片,@性別,@出生日期,@籍貫,@工齡,@電話,");//追加文字內容
                strSql.Append("@部門名稱,@技術職稱,@婚姻狀態,@健康狀態)");//追加文字內容
                SqlCommand cmd = new SqlCommand(strSql.ToString(), con);//建立資料庫命令物件
                cmd.Parameters.Add("@檔案編號", SqlDbType.Text).Value =//新增引數並賦值
                    this.textBox1.Text.Trim().ToString();
                cmd.Parameters.Add("@工號", SqlDbType.Text).Value =//新增引數並賦值
                    this.textBox2.Text.Trim().ToString();
                cmd.Parameters.Add("@姓名", SqlDbType.Text).Value =//新增引數並賦值
                    this.textBox3.Text.Trim().ToString();
                cmd.Parameters.Add("@照片", SqlDbType.Text).Value = strPath;//新增引數並賦值
                cmd.Parameters.Add("@性別", SqlDbType.Text).Value =//新增引數並賦值
                    this.comboBox1.Text.Trim().ToString();
                cmd.Parameters.Add("@出生日期", SqlDbType.Text).Value =//新增引數並賦值
                    this.textBox4.Text.Trim().ToString();
                cmd.Parameters.Add("@籍貫", SqlDbType.Text).Value =//新增引數並賦值
                    this.textBox5.Text.Trim().ToString();
                cmd.Parameters.Add("@工齡", SqlDbType.Int).Value =//新增引數並賦值
                    Convert.ToInt16(this.textBox6.Text.Trim().ToString());
                cmd.Parameters.Add("@電話", SqlDbType.Text).Value =//新增引數並賦值
                    this.textBox6.Text.Trim().ToString();
                cmd.Parameters.Add("@部門名稱", SqlDbType.Text).Value =//新增引數並賦值
                    this.textBox7.Text.Trim().ToString();
                cmd.Parameters.Add("@技術職稱", SqlDbType.Text).Value =//新增引數並賦值
                    this.comboBox2.Text.Trim().ToString();
                cmd.Parameters.Add("@婚姻狀態", SqlDbType.Text).Value =//新增引數並賦值
                    this.comboBox3.Text.Trim().ToString();
                cmd.Parameters.Add("@健康狀態", SqlDbType.Text).Value =//新增引數並賦值
                    this.comboBox4.Text.Trim().ToString();
                cmd.ExecuteNonQuery();//執行SQL語句
                con.Close();//關閉資料庫連線
                return true;//方法返回布林值
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示!");
                return false;//方法返回布林值
            }
        }

        #region//訪問器
        private string 檔案編號;
        private string 工號;
        private string 姓名;
        private string 照片;
        private string 性別;
        private string 出生日期;
        private string 籍貫;
        private int 工齡;
        private string 電話;
        private string 部門名稱;
        private string 技術職稱;
        private string 婚姻狀態;
        private string 健康狀態;

        private string ID
        {
            get { return 檔案編號; }
            set { 檔案編號 = value; }
        }
        private string NumID
        {
            get { return 工號; }
            set { 工號 = value; }
        }
        private string name
        {
            get { return 姓名; }
            set { 姓名 = value; }
        }
        private string pic
        {
            get { return 照片; }
            set { 照片 = value; }
        }
        private string sex
        {
            get { return 性別; }
            set { 性別 = value; }
        }
        private string birthday
        {
            get { return 出生日期; }
            set { 出生日期 = value; }
        }
        private string city
        {
            get { return 籍貫; }
            set { 籍貫 = value; }
        }
        private int years
        {
            get { return 工齡; }
            set { 工齡 = value; }
        }
        private string phone
        {
            get { return 電話; }
            set { 電話 = value; }
        }
        private string part
        {
            get { return 部門名稱; }
            set { 部門名稱 = value; }
        }
        private string state
        {
            get { return 技術職稱; }
            set { 技術職稱 = value; }
        }
        private string marriage
        {
            get { return 婚姻狀態; }
            set { 婚姻狀態 = value; }
        }
        private string health
        {
            get { return 健康狀態; }
            set { 健康狀態 = value; }
        }

        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


    }
}