使用C#在数据库中添加新数据 [英] Add new data in database using C#

查看:167
本文介绍了使用C#在数据库中添加新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

Here is my code

public void beginTrans()
        {
            try
            {
                if (dbTran == null)
                {
                    if (dbConn.State == 0) { createConn(); }
                    dbTran = dbConn.BeginTransaction();
                    dbCommand.Transaction = dbTran;
                    dbAdapter.SelectCommand.Transaction = dbTran;
                    dbAdapter.InsertCommand.Transaction = dbTran;
                    dbAdapter.UpdateCommand.Transaction = dbTran;
                    dbAdapter.DeleteCommand.Transaction = dbTran;
                }
            }
            catch (Exception exc) { throw exc; }
        }

        public void commitTrans()
        {
            try
            {
                if (dbTran != null)
                {
                    dbTran.Commit();
                    dbTran = null;
                }
            }
            catch (Exception exc) { throw exc; }
        }

        public void rollBackTrans()
        {
            try
            {
                if (dbTran != null)
                {
                    dbTran.Rollback();
                    dbTran = null;
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }





public DataSet insertQuery(DataSet ds, Hashtable ht)
        {
            try
            {
                dTable = ds.Tables[0];
                dRow = dTable.NewRow();
                object value;
                foreach (string ha in ht.Keys)
                {
                    value = ht[ha];
                    dRow[ha] = value;
                }
                dTable.Rows.Add(dRow);
                return ds;
            }
            catch (Exception exc) { throw exc; }
        }







public override void insert_lavlah(Hashtable hash)
        {
            try
            {
                dAccess.beginTrans();
                dSet.Clear();
                getDSet = dAccess.insertQuery(dSet, hash);
                dAccess.commitTrans();
            }
            catch (Exception exc)
            {
                dAccess.rollBackTrans();
                throw exc;
            }
        }





private void btnOk_zereg_Click(object sender, EventArgs e)
        {
            // bagshiin zeregt ogogdol nemeh
            //zereg.save_lavlah();
            Hashtable hTab = new Hashtable();
            hTab.Add("zereg_code", Convert.ToInt32(txtID_addZereg.Text));
            hTab.Add("zereg_ner", txtName_addZereg.Text);
            zereg.insert_lavlah(hTab);
            txtID_addZereg.Clear();
            txtName_addZereg.Clear();
            gbox_addZereg.Visible = false;

            //zereg.save_lavlah();
        }



我的错在哪里!
错误信息:对象引用未设置为对象的实例.



Where is my fault!!!
ERROR IS: Object reference not set to an instance of an object.

推荐答案

遇到此类问题时,通常很容易解决-对我们来说并不容易.

我们无法确切告诉您问题出在哪里-您无法告诉我们发生异常的那一行-但是调试器是解决此问题的非常好的工具.
在VS中,在开始测试之前,请转到菜单栏,然后​​在"Debug ... Exception ..."下查找.
在出现的对话框中,在每个复选框中打勾,尤其是在"throw"列中的复选框.
按确定

现在运行您的应用程序.当获取空引用错误时,调试器将停止并确切显示导致错误的行.现在,您可以检查该行并查找包含null的变量.现在由您来找出为什么它为空-但至少您会知道它是什么! :laugh:
When you get this kind of problem, it is normally pretty easy for you to sort out - not so easy for us.

We can''t tell exactly where your problem is - you don''t tell us which line you get the exception on - but the debugger is a very good tool for working this out.
In VS, before you start testing, go to the menu bar, and look under "Debug...Exceptions..."
In the dialog that results, put a tick in every single check box, especially those in the "thrown" column.
Press OK

Now run your app. When the get the null reference error, the debugger will stop and show you exactly which line caused it. You can now examine the line and look for a variable containing a null. It''s now up to you to find out why it is null - but at least you will know which it is! :laugh:


这篇关于使用C#在数据库中添加新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆