C# 保存(新增、修改)

2021/9/4 12:35:46

本文主要是介绍C# 保存(新增、修改),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

源码

        //保存
        private void update_Click(object sender, EventArgs e)
        {
            gridView1.CloseEditor();
            gridView1.UpdateCurrentRow();
            DataTable dt = (DataTable)gridControl1.DataSource;
            //选中行行号
            int a = gridView1.FocusedRowHandle;
           
            if ( GSR_Fid() != "")
            {
                
                if (MessageBox.Show("确实要保存该行吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    String connsql = "server=localhost.;database=M10;integrated security=SSPI";
                    SqlConnection MyConnection = new SqlConnection(connsql);
                    string sql_update = "Update m_task_001 set FItemID='" + dt.Rows[a]["FItemID"] + "',FBillNo='" + dt.Rows[a]["FBillNo"]
                        + "',FStatus='" + dt.Rows[a]["FStatus"] + "',FPQty='" + dt.Rows[a]["FPQty"] + "'" + " where FID=" + "'" + GSR_Fid() + "'";
                    SqlCommand MyCommand = new SqlCommand(sql_update, MyConnection);
                    try
                    {
                        MyConnection.Open();
                        MyCommand.ExecuteNonQuery();
                        MyConnection.Close();
                        MessageBox.Show("成功修改信息!");
                    }
                    catch (Exception ex)
                    {
                        //写入TXT文本
                        string Route = @"D:\1.txt";
                        StreamWriter sw = File.AppendText(Route);
                        sw.Write(ex.Message + "        " + DateTime.Now.ToString() + "\r\n");
                        sw.Close();
                        MessageBox.Show("错误信息:" + ex.Message, "出现错误");
                        return;
                    }
                }
            }
            else
            {
                if (MessageBox.Show("确实要保存该行吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    String MyConn = "server=localhost.;database=M10;integrated security=SSPI";
                    SqlConnection MyConnection = new SqlConnection(MyConn);
                    string sql_insert = "insert into m_task_001 (FItemID,FBillNo,FStatus,FPQty) values ('" + dt.Rows[a]["FItemID"] + "','"
                        + dt.Rows[a]["FBillNo"] + "','" + dt.Rows[a]["FStatus"] + "','" + dt.Rows[a]["FPQty"] + "')";
                    SqlCommand MyCommand = new SqlCommand(sql_insert, MyConnection);
                    try
                    {
                        MyConnection.Open();
                        MyCommand.ExecuteNonQuery();
                        MyConnection.Close();
                        MessageBox.Show("成功添加信息!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("错误信息:" + ex.Message, "出现错误");
                    }
                }
                
            }
            CNT();
        }


这篇关于C# 保存(新增、修改)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程