使用文本框将行写入基于服务的数据库 [英] Write line to Service-Based Database with textbox

查看:82
本文介绍了使用文本框将行写入基于服务的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用文本框逐行创建和写入基于服务的数据库,而没有其他列(仅索引和短语).因此,当初学者需要帮助或有用的示例来弄清楚我的情况时,我在这里做错了什么.

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
)

在"ID"列的属性身份规范"中,我已将(身份)更改为True,然后更新了dbo.User [Design],然后刷新至我的SampleDatabase.mdf,然后在用户"选项卡上显示了表数据",然后将新项LINQ作为数据类添加到我的项目的SQL类中,并通过以下方式将其添加到Program.cs目录AppDomain.CurrentDomain.SetData("DataDirectory", System.Environment.CurrentDirectory.Replace("\\bin\\Debug", ""));中:

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());
        }
    }
}

这是我的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;
        }
    }
}

所以我已将消息替换为MessageBox.Show(ex.Message + ":" + ex.StackTrace);,但不确定其含义和必须做的事情:

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

如果身份规范为False,则只向数据库写一行,但在第二行中,我收到此消息:

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

推荐答案

我以评论的形式发布,但我认为这实际上是一个答案. 因此,我将添加一些更多的解释:

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

如果您没有明确地对数据库说可以通过将INSERT_IDENTITY设置为On来向Identity列插入值,那么数据库就是它的所有者,而不是您. 您可以停止尝试插入用户ID并让数据库为您处理它,也可以将INSERT_IDENTITY设置为On并完成工作.如果未将ID设置为身份"列,请确保没有重复的ID,否则将崩溃,如第二个错误所述.

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.

信用: IDENTITY_INSERT设置为OFF的问题? :-/

这篇关于使用文本框将行写入基于服务的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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