无法向数据表添加新行[c#]

时间:2016-08-21 19:14:37

标签: c# xml datatable dataset datarow

我有一个数据集,它存储在XML文件中。当应用程序启动并且XML文件尚不存在时,我想在我的数据集中添加一些行并将其保存为XML。我使用以下代码来执行此操作:

        partiesDb = new Parties();

        //...
        DataTable partyDsTable = partiesDb.Tables["party"]; //partiesDb is DataSet object
        for (int i = 0; i < size; i++)
        {
            DataRow row = partyDsTable.NewRow(); // HERE THE CODE STOPS
            row["sth"] = sth;
            row["sth"] = "0";
            row["id"] = "1";
            if (!partyDsTable.Rows.Contains(row))
            {
                partyDsTable.Rows.Add(row);
            }
        }
        System.Windows.Forms.MessageBox.Show(partyDsTable.Rows[0]["sth"].ToString());
        partiesDb.WriteXml(path);

没有任何反应。甚至消息框也没有显示。该功能在创建新行时停止,我不知道为什么。你能救我吗?

[编辑]: 这段代码工作正常:

            partiesDb = new Parties();
            DataTable dsTable = partiesDb.Tables["party"];
            DataRow dsRow = dsTable.NewRow();
            dsRow["name"] = "";
            dsRow["id"] = "";
            dsRow["votes"] = "";
            dsTable.Rows.Add(dsRow);
            partiesDb.WriteXml(partiesDbPath);

我不知道为什么会这样,但是当我添加循环时它不会......

3 个答案:

答案 0 :(得分:1)

您无法向null变量添加内容。因此,如果它为空,则需要实例化partyDsTable

DataTable partyDsTable = partiesDb.Tables["party"]; //partiesDb is DataSet object

if(partyDsTable == null) // instantiate it
    partyDsTable = new DataTable();

for (int i = 0; i < size; i++)
{
    ....

答案 1 :(得分:0)

DataTable partyDsTable = partiesDb.Tables["party"]; //partiesDb is DataSet object
    for (int i = 0; i < size; i++)// "size" does not look like it's set to anything. If "size" is 0, then the loop will not loop. 
    {

            partyDsTable.Rows.Add(new object[] {sth,"0","1"});//you should probably make the columns into the correct type if you are using integers.

    }
    System.Windows.Forms.MessageBox.Show(partyDsTable.Rows[0]["sth"].ToString());
    partiesDb.WriteXml(path);

答案 2 :(得分:0)

此外,如果您创建的字段与数据库表中的项目匹配,您可以使用反射执行各种操作,以使您的数据始终良好:

public static DataTable create_DataTable_From_Generic_Class(Type t)
    {
        DataTable d = new DataTable();
        FieldInfo[] fI = t.GetFields();
        for(int i = 0; i < fI.Length; i++)
        {
            DataColumn dC = new DataColumn(fI[i].Name, fI[i].FieldType);
            d.Columns.Add(dC);
        }
        return d;
    }
    public static object[] Create_Datatable_Row_From_Generic_Class(Type t, object instance,DataTable dt)
    {

        FieldInfo[] f = t.GetFields();
        object[] ret = new object[f.Length];
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            ret[i] = t.GetField(dt.Columns[i].ColumnName).GetValue(instance);

        }
        return ret;

    }
    public static List<string[]> getParams(Type type, bool convertToSQL)
    {


        List<string[]> ret = new List<string[]>();
        Dictionary<string, object> properties = new Dictionary<string, object>();
        foreach (FieldInfo prop in type.GetFields())
            properties.Add(prop.Name, prop.FieldType);
        foreach (string key in properties.Keys)
        {
            string[] r = { key, properties[key].ToString().Replace("System.", "") };
            ret.Add(r);
        }
        if (convertToSQL)
        {
            return convertFromNetToSQLDataTypes(ret);

        }
        else
        {
            return ret;
        }
    }
    private static List<string[]> convertFromNetToSQLDataTypes(List<string[]> list)
    {
        foreach (string[] data in list)
        {
            data[1] = data[1].Replace("Int16", "tinyint");
            data[1] = data[1].Replace("Int32", "int");
            data[1] = data[1].Replace("Int64", "bigint");
            data[1] = data[1].Replace("Double", "float");
            data[1] = data[1].Replace("Boolean", "tinyint");
            data[1] = data[1].Replace("Double", "float");
            data[1] = data[1].Replace("Long", "bigint");
            data[1] = data[1].Replace("String", "varchar(100)");
        }
        return list;
    }
    public static object[] sql_Reader_To_Type(Type t, SqlDataReader r)
    {
        List<object> ret = new List<object>();
        while (r.Read())
        {
            FieldInfo[] f = t.GetFields();
            object o = Activator.CreateInstance(t);
            for (int i = 0; i < f.Length; i++)
            {
                string thisType = f[i].FieldType.ToString();
                switch (thisType)
                {
                    case "System.String":

                        f[i].SetValue(o, Convert.ToString(r[f[i].Name]));
                        break;
                    case "System.Int16":
                        f[i].SetValue(o, Convert.ToInt16(r[f[i].Name]));
                        break;
                    case "System.Int32":
                        f[i].SetValue(o, Convert.ToInt32(r[f[i].Name]));
                        break;
                    case "System.Int64":
                        f[i].SetValue(o, Convert.ToInt64(r[f[i].Name]));
                        break;
                    case "System.Double":
                       // Console.WriteLine("converting " + f[i].Name + " to double");
                        double th;
                        if (r[f[i].Name] == null)
                        {
                            th = 0;
                        }
                        else
                        {
                            if (r[f[i].Name].GetType() == typeof(DBNull))
                            {
                                th = 0;
                            }
                            else
                            {
                                th = Convert.ToDouble(r[f[i].Name]);
                            }
                        }
                        try { f[i].SetValue(o, th); }
                        catch (Exception e1)
                        {
                            throw new Exception("can't convert " + f[i].Name + " to doube - value =" + th);
                        }
                        break;
                    case "System.Boolean":
                        f[i].SetValue(o, Convert.ToInt32(r[f[i].Name]) == 1 ? true : false);
                        break;
                    case "System.DateTime":
                        f[i].SetValue(o, Convert.ToDateTime(r[f[i].Name]));
                        break;
                    default:
                        throw new Exception("Missed data type in sql select in getClassMembers class line 73");

                }
            }
            ret.Add(o);

        }
        return ret.ToArray();


    }

不断。您可以使用这样的反射来创建与您的类匹配的表,自动创建插入或过程调用,从不使用不正确的数据类型或再次使字段无序。