使用C#在数据库中存储数据 [英] Storing data in a database in C#

查看:286
本文介绍了使用C#在数据库中存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我用Windows窗体开发了一个项目,这个项目会从数据库中生成真假问题。问题显示在文本框中的表格上,有两个单选按钮,一个用于TRUE,另一个用于FALSE,用户可以选择。是否有更简单的方法可以将用户选择的答案存储到数据库表中。



我的意思是我知道如何将所选答案插入数据库表中但我唯一的问题是如果我有10个问题和5个用户回答问题,这是意味着我必须为每个用户创建一个表,以便插入问题和选择答案..或者我是否必须在一个表中插入详细信息并具有冗余数据?





我真的希望这是有道理的,我不是要求任何人为我做这件事..我只需要点子想法



谢谢大家

解决方案

为了比SA已经回答的更具体,你需要创建一个名为answers的表,或类似的东西。你需要一个user_id列,一个question_id,然后是一个答案。



因此,如果您需要找到给定用户的所有答案,您可以根据user_id进行查询,或者如果您想查看给定问题的所有答案,您可以在question_id等上查询


你需要学习dababase设计(模式)和数据建模的创建,这是一个非常大的主题,但......有些阅读:

http://en.wikipedia.org/wiki/Single_Point_of_Truth [ ^ ](立即回答你的一个问题),

http://en.wikipedia.org/wiki/Database_schema [ ^ ],

http ://en.wikipedia.org/wiki/Database_normalization [ ^ ],

http://en.wikipedia.org/wiki/Data_modeling [ ^ ],

http://en.wikipedia.org/wiki/Database_integrity [ ^ ]。



在实践中,对于像你这样的许多简单问题,人们可以通过经验快速发展正确的架构和模型。对于其他一些问题,至少可以说需要很多专业性才是非常困难的问题。



-SA

Hi guys,

I have developed a project using windows forms, this project generates true or false questions from the database. The questions are displayed on a form in textbox and there two radio buttons one for "TRUE" and the other for "FALSE" to be selected by the user. is there simpler way I can store the answers that have selected by users into a database table.

I mean I know how I can insert the selected answer into a database table but my only issue that if I have got 10 questions and 5 users answering questions, does this mean I have to create a table for each and every user in order to insert the question and the select answer .. or will I have to insert the details in one table and have redundancy data?


I really really hope this makes sense and I am not asking anyone to do it for me.. I just need ideas

Thank you guys

解决方案

To be more specific than what SA already answered, you'll want to create a table named answers, or something like that. You'll need a column for the user_id, the question_id, and then the answer.

So, if you ever need to find all answers for a given user you can query based on user_id or if you want to see all answers for a given question you can query on question_id, etc.


You need to learn creation of dababase design (schema) and data modeling, this is quite a big topic, but… some reading:
http://en.wikipedia.org/wiki/Single_Point_of_Truth[^] (immediate answer to one of your questions),
http://en.wikipedia.org/wiki/Database_schema[^],
http://en.wikipedia.org/wiki/Database_normalization[^],
http://en.wikipedia.org/wiki/Data_modeling[^],
http://en.wikipedia.org/wiki/Database_integrity[^].

In practice, for many simple problems, like yours, people develop the sense of right schema and model pretty quickly, with experience. For some other problems, it could be very difficult issue requiring a lot of professionalism, to say the least.

—SA


这篇关于使用C#在数据库中存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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