c#ado.net无法将长字符串保存到sql数据库

时间:2017-05-29 06:42:29

标签: c# sql-server ado.net

我试图以文本形式输入并将其保存到SQL表中, 如果字符串不太长,代码工作正常,但当你超过大约70个字符时,SaveChanges();命令无法保存数据。 SQL数据类型设置为nvarchar(max)。有什么想法吗?

private void btnSave_Click(object sender, EventArgs e)
{
    Model.TBL_USERS USR = new Model.TBL_USERS();
    USR.USR_NAME = txtNAME.Text;
    USR.USR_LAST = txtLAST.Text;
    USR.USR_MOBILE = txtMOBILE.Text;
    USR.USR_TEL1 = txtTEL1.Text ;
    USR.USR_ADDRESS = txtADDRESS.Text;
    USR.USR_COMPANY = txtCOMPANY.Text;
    //USR.USR_COMPANY = "=chrome..69i57.13150j0j4&sourceid=chrome&ie=UTF-8";
    DB.TBL_USERS.Add(USR);
    DB.SaveChanges();

    dataGridView1.DataSource = "";
    dataGridView1.DataSource = DB.TBL_USERS.ToList();
}

这是解决方案中的整个代码:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Model.SAMA_User_CRM_V1 DB = new Model.SAMA_User_CRM_V1();
    int IDGRID;
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = DB.TBL_USERS.ToList();

    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        Model.TBL_USERS USR = new Model.TBL_USERS();
        USR.USR_NAME = txtNAME.Text;
        USR.USR_LAST = txtLAST.Text;
        USR.USR_MOBILE = txtMOBILE.Text;
        USR.USR_TEL1 = txtTEL1.Text ;
        USR.USR_ADDRESS = txtADDRESS.Text;
        USR.USR_COMPANY = txtCOMPANY.Text;
        //USR.USR_COMPANY = "=chrome..69i57.13150j0j4&sourceid=chrome&ie=UTF-8";
        DB.TBL_USERS.Add(USR);
        DB.SaveChanges();


        dataGridView1.DataSource = "";
        dataGridView1.DataSource = DB.TBL_USERS.ToList();
    }

    private void btnDELETE_Click(object sender, EventArgs e)
    {
        var qdel = DB.TBL_USERS.Where(x => x.ID == IDGRID).ToList();

        foreach (var item in qdel)
        {
            DB.TBL_USERS.Remove(item);

        }
        DB.SaveChanges();
        dataGridView1.DataSource = "";
        dataGridView1.DataSource = DB.TBL_USERS.ToList();
    }

    private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
        IDGRID = (int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;

    }
}

}

1 个答案:

答案 0 :(得分:0)

考虑到我发现问题所在的评论:

最初,模型是使用使用nvarchar(50)的SQL表创建的,在将其更改为nvarchar(500)nvarchar(max)之后需要更新model.edmx ...更新后模特所有的东西都像魅力一样。