您的位置:首页 > 其它

修改窗口模板code

2013-04-02 21:31 417 查看
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 meifengSystem

{

public partial class shouhuoMForm : Form

{

//common

public Form pForm = null;

public string PID = "";

string DRIVER = "Server=127.0.0.1;UID=sa;PWD=admin;DataBase=factory";

SqlConnection conn = null;

SqlDataAdapter da = null;

DataSet ds = new DataSet();



//private

decimal price;

decimal amount;

public shouhuoMForm()

{

InitializeComponent();

}

private void shouhuoMForm_Load(object sender, EventArgs e)

{

init();

}





private void BN_modef_Click(object sender, EventArgs e)

{

if (checkForm() == true)

{

DialogResult result = MessageBox.Show("确定修改吗?", "修改", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result == DialogResult.Yes)

{

if (deleteRow(L_PID.Text) !=0)

{

if (addShouhuoRow(L_PID.Text) != 0)

{

MessageBox.Show("修改成功!");

}

else

{

MessageBox.Show("修改失败!");

}

}

else

{

MessageBox.Show("修改失败!");

}



}

}

}



private void shouhuoMForm_FormClosed(object sender, FormClosedEventArgs e)

{

FormClose();

}

//---------Common---------------------------------------------------------------------

public void init()

{

LoadData();

L_PID.Visible = false;

Load_CB_Remark();

}

public void LoadData()

{

if (PID == "")

{

MessageBox.Show("请先选择要修改的行!");

this.Hide();

this.Dispose();

}

else

{

try

{

conn = new SqlConnection(DRIVER);

string MainStr = "select s.id, kehu_name, doctor_name, yamo_no, ya_type, yawi, color, remark_id, remark_text, price, amount, yewuyuan, product_no, shouhuo_date, huanzhe, fangong_yuanyin from shouhuo s left join remark r on s.remark_id=r.id
where s.id='" + PID + "'";

da = new SqlDataAdapter(MainStr, conn);

da.Fill(ds);

L_PID.Text = ds.Tables[0].Rows[0]["id"].ToString();

TB_amount.Text = ds.Tables[0].Rows[0]["amount"].ToString();

TB_color.Text = ds.Tables[0].Rows[0]["color"].ToString();

TB_doctor_name.Text = ds.Tables[0].Rows[0]["doctor_name"].ToString();

TB_fangong_yuanyin.Text = ds.Tables[0].Rows[0]["fangong_yuanyin"].ToString();

TB_huanzhe.Text = ds.Tables[0].Rows[0]["huanzhe"].ToString();

TB_kehu_name.Text = ds.Tables[0].Rows[0]["kehu_name"].ToString();

TB_price.Text = ds.Tables[0].Rows[0]["price"].ToString();

TB_product_no.Text = ds.Tables[0].Rows[0]["product_no"].ToString();

TB_ya_type.Text = ds.Tables[0].Rows[0]["ya_type"].ToString();

TB_yamo_no.Text = ds.Tables[0].Rows[0]["yamo_no"].ToString();

TB_yawi.Text = ds.Tables[0].Rows[0]["yawi"].ToString();

TB_yewuyuan.Text = ds.Tables[0].Rows[0]["yewuyuan"].ToString();

CB_remark.SelectedValue = int.Parse(ds.Tables[0].Rows[0]["remark_id"].ToString());

DT_shouhuo_date.Value = Convert.ToDateTime(ds.Tables[0].Rows[0]["shouhuo_date"].ToString());

}

catch (Exception err)

{

MessageBox.Show("数据库出错" + err.Message);

}

finally

{

conn.Close();

}

}

}

public void Load_CB_Remark()

{

conn = new SqlConnection(DRIVER);

da = new SqlDataAdapter("select * from remark", conn);

da.Fill(ds, "table");

try

{

CB_remark.DataSource = ds.Tables["table"];

CB_remark.ValueMember = "id";

CB_remark.DisplayMember = "remark_text";

}

catch (Exception err)

{

MessageBox.Show("load备注类型出错:" + err.Message);

}

}

public bool checkForm()

{

bool result = true;

if (TB_price.Text.Trim() != "" && (System.Decimal.TryParse(TB_price.Text.Trim(), out price) == false))

{

MessageBox.Show("单价输入不是合法的");

result = false;

}

if (TB_amount.Text.Trim() != "" && (System.Decimal.TryParse(TB_amount.Text.Trim(), out amount) == false))

{

MessageBox.Show("金额输入不是合法的");

result = false;

}

return result;

}

public int deleteRow(string ID)

{

int s = 0;

string SQL_Delete = "delete from shouhuo where id ='" + ID + "'";

s = Execute(SQL_Delete);

return s;

}

public int addShouhuoRow(string ID)

{

int s = 0;

string InsertSql = "insert into shouhuo(id,kehu_name, doctor_name, yamo_no, ya_type, yawi, color, remark_id, price, amount, yewuyuan, product_no, shouhuo_date, huanzhe, fangong_yuanyin)"

+ "values('"

+ ID + "','"

+ TB_kehu_name.Text + "','"

+ TB_doctor_name.Text + "','"

+ TB_yamo_no.Text + "','"

+ TB_ya_type.Text + "','"

+ TB_yawi.Text + "','"

+ TB_color.Text + "',"

+ Convert.ToInt32(this.CB_remark.SelectedValue) + ","

+ price + ","

+ amount + " ,'"

+ TB_yewuyuan.Text + "','"

+ TB_product_no.Text + "','"

+ DT_shouhuo_date.Value.Date + "','"

+ TB_huanzhe.Text + "','"

+ TB_fangong_yuanyin.Text + "')";

s = Execute(InsertSql);

return s;

}

public void FormClose()

{

shouhuoForm sf = (shouhuoForm)this.pForm;

sf.dv1Load();

}

public int Execute(string SQL)

{

conn = new SqlConnection(DRIVER);

SqlCommand cmd = new SqlCommand(SQL, conn);

int i = 0;

try

{

conn.Open();

i =cmd.ExecuteNonQuery();

}

catch (Exception err)

{

Console.WriteLine("数据库出错" + err.Message);

}

finally

{

conn.Close();

}

return i;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: