我想在mysql中显示一个文本文件...它显示空id不允许..做什么..帮助 [英] i want to display a text file in mysql... its showing null id not allowed.. what to do.. help

查看:61
本文介绍了我想在mysql中显示一个文本文件...它显示空id不允许..做什么..帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 MySql.Data.MySqlClient;
using System.IO;

namespace texttodb3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines("c:Users\\Techsoft\\Desktop\\db.txt");
            foreach (var line in lines)
            {
                var data = line.Split(new[] { ',' }, 4);
                int Id = int.Parse(data[0].Trim());
                int age = int.Parse(data[1].Trim());

                string name = data[2].Trim();
                StoreRecord(Id, age, name);
            }

        }
        private void StoreRecord(int Id, int age, string name)
        {
            var conStr = "server=localhost; database=neww; password=techsoft; User Id=root;";
            using (var connection = new MySqlConnection(conStr))
            using (var command = connection.CreateCommand())
            {
                connection.Open();
                command.CommandText =
                @"INSERT INTO neww
            (Id, age, name)
          VALUES
            (@Id, @age, @name)";
                command.Parameters.AddWithValue("@Id", Id);
                command.Parameters.AddWithValue("@age", age);
                command.Parameters.AddWithValue("@name", name);

                command.ExecuteNonQuery();
            }
        }
    }
}

推荐答案

代码没有看起来它有一个主要问题 - 但错误信息暗示它是一个DB问题。我之所以这么说是因为ID字段被赋予了一个int - 这是一个值类型而不能为null,所以null id not allowed消息必须来自表中的另一列。



首先在StoreRecord的第一行放一个断点并查看三个参数 - 我打赌他们没事。

因此,单步执行代码,如果错误出现在ExecuteNonQuery语句中,那么您需要先查看 neww 表定义并确保它只有三列。我很确定你有四分之一没有设置。
The code doesn''t look like it has a major problem - but the error message implies that it is a DB problem. The reason I say that is that the ID field is being given an int - which is a value type and cannot be null, so the "null id not allowed" message must be coming from a different column in your table.

Start by putting a breakpoint on the first line in StoreRecord and looking at the three parameters - I''m betting they are ok.
So single step through the code, and if the error comes at the ExecuteNonQuery statement then you need top look at your neww table definition and ensure it only has three columns. I''d be pretty sure there is a fourth that you aren''t setting.


这篇关于我想在mysql中显示一个文本文件...它显示空id不允许..做什么..帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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