Write line to Service-Based Database with textbox

China☆狼群 提交于 2020-01-07 02:18:12

问题


I'm trying to create and write line by line to the Service-Based Database with textbox, without additional columns just Index and phrase. So as beginner need help or useful example for my case to figure out, what I'm doing wrong here.

So what I did, my database:

CREATE TABLE [dbo].[User]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [Phrase] TEXT NOT NULL
)

In Id column Properties Identity Specification I’ve changed (is identity) to True, then Update dbo.User [Design], then refresh to my SampleDatabase.mdf , then Show Table Data my tab User, and then I’ve added new item LINQ to SQL Classes to my project as DataClass and I’ve added to my Program.cs directory AppDomain.CurrentDomain.SetData("DataDirectory", System.Environment.CurrentDirectory.Replace("\\bin\\Debug", "")); this way:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XX_7_DATABASE_LOCAL
{
    static class Program
    {
        [STAThread]
        static void Main()
        {   
            AppDomain.CurrentDomain.SetData("DataDirectory", System.Environment.CurrentDirectory.Replace("\\bin\\Debug", ""));  
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

And here my Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            User newUser = new User();
            newUser.Phrase = (textBox1.Text);

            DataClassDataContext dbCtx = new DataClassDataContext();
            dbCtx.Users.InsertOnSubmit(newUser);

            try
            {
                dbCtx.SubmitChanges();
                MessageBox.Show("Successful!");
            }
            catch(Exception ex)
            {
                MessageBox.Show("Fail!");
            }        
        }

        private void button2_Click(object sender, EventArgs e) 
        {
            DataClassDataContext dbContext = new DataClassDataContext();
            var getData = (
                from x in dbContext.Users
                select x
                );

            dataGridView1.DataSource = getData;
        }
    }
}

So I’ve replaced message to MessageBox.Show(ex.Message + ":" + ex.StackTrace);, but not sure what it means and what I have to do:

if Identity Specification is False, writes only one line to database, but with second one, I got this message:


回答1:


I posted as a comment, but I think it is actually an answer. So I will add some more explanation:

If you are not explicitly saying to the database that you can insert a value to an Identity column by setting INSERT_IDENTITY to On, then the database is its owner, not you. You can either stop trying to insert an user id and let the database handle it for you or you can set INSERT_IDENTITY to On and do the job. If you don't set the id to be an Identity column, then make sure that there will be no duplicated ids, otherwise it will crash, as described in the second error.

I would go for the first option and let the database do that for you.

Credits : Problems with IDENTITY_INSERT Set to OFF? :-/



来源:https://stackoverflow.com/questions/39152801/write-line-to-service-based-database-with-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!