将值插入数据库 [英] insert the values into database

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

问题描述

数据库如下



Field_Name数据类型



课程是否有用int

课程工作人员是否准时工作

是否需要实用工具

你们对我们学院感到满意吗





设计如下



评级优秀/良好/公平/可怜



优秀是指评级1

好​​的意思是评级2

公平意味着评级3

差评意味着评级4



课程是否有用dropdownlist1(优秀/优秀/公平/差)

课程工作人员是否准时dropdownlist2(优秀/良好/公平/差)

是否需要实用性dropdownlist3(优秀/良好/公平/差)

您对我们学院的下拉列表4感到高兴(优秀/优秀/公平/差)





例如在运行模式下用户选择r 4下拉列表如下



课程是否有用dropdownlist1(优秀)

课程工作人员是否准时dropdownlist2(好)

是否需要实用性dropdownlist3(公平)

你们很高兴我们学院dropdownlist4(差)



和用户点击数据库记录中的提交按钮如下



课程是否有用1(优秀)

是否课程人员准时2(好) )

是否需要实用性3(一般)

你很高兴我们学院4(可怜)



为此我怎么能在asp.net中使用csharp。

问候,

Narasiman P.

Database as follows

Field_Name Datatype

whether the course is useful int
whether course staff on time int
whether practicals needed int
Are u happy to our institute int


Design as follows

Rating Excellent/Good/Fair/Poor

Excellent means rating 1
Good means rating 2
Fair means rating 3
Poor means rating 4

whether the course is useful dropdownlist1 (Excellent/Good/Fair/Poor)
whether course staff on time dropdownlist2 (Excellent/Good/Fair/Poor)
whether practicals needed dropdownlist3 (Excellent/Good/Fair/Poor)
Are u happy to our institute dropdownlist4 (Excellent/Good/Fair/Poor)


for example in run mode user select the rating of 4 Dropdownlist as follows

whether the course is useful dropdownlist1 (Excellent)
whether course staff on time dropdownlist2 (Good)
whether practicals needed dropdownlist3 (Fair)
Are u happy to our institute dropdownlist4 (Poor)

and when user clicks the submit button in database record as follows

whether the course is useful 1 (Excellent)
whether course staff on time 2 (Good)
whether practicals needed 3 (Fair)
Are u happy to our institute 4 (Poor)

for that how can i do in asp.net using csharp.
Regards,
Narasiman P.

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!


你是否要求我们给你如何设计应用程序的指示,或者你的代码的某些方面是否会导致问题?您的主题表明问题与如何插入值有关,但您的描述似乎不支持这一点。如果您要查找的是如何插入值,那么我通常会看到一个看起来像这样的方法(这里我假设您有一个包含结果的表 - 从您的描述中得到合理的假设,但是由于我会在一分钟内解释的原因,设计非常糟糕):
Are you asking for us to give you pointers on how to design the application or is there some aspect of your code that is causing you problems? Your subject indicates that the issue has to do with how the values would be inserted, but your description doesn't seem to support that. If all you are looking for is how to insert the values, then I would generally look at having a method that looked like this (here I'm assuming you have a single table containing the results - a reasonable assumption from your description, but a pretty poor design for reasons I'll explain in a minute):
public void SaveRatings(int userId, int course, int staff, int practicals, int institute)
{
  using (SqlConnection connection = new SqlConnection(...connectionstring goes here ...)
  {
    using (SqlCommand cmd = new SqlCommand("INSERT INTO MyTable(USER_ID, COURSE, STAFF, PRACTICAL, INSTITUTE) VALUES (@UserID, @Course, @Staff, @Practical, @Institute)")
    {
      cmd.Connection = connection;
      cmd.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int).Value = userId;
      cmd.Parameters.Add(new SqlParameter("@Course", SqlDbType.Int).Value = course;
      cmd.Parameters.Add(new SqlParameter("@Staff", SqlDbType.Int).Value = staff;
      cmd.Parameters.Add(new SqlParameter("@Practical", SqlDbType.Int).Value = practical;
      cmd.Parameters.Add(new SqlParameter("@Institute", SqlDbType.Int).Value = institute;

      cmd.ExecuteNonQuery();
    }
  }
}

因此,虽然该查询可行,但问题是为什么我认为该表是一个糟糕的设计?嗯,最大的问题是它非常不灵活。换句话说,一旦你推出它,你就无法在不重新访问整个应用程序的情况下添加新的评级类型。假设您想要添加关于社交生活质量的评级;如果你这样做,你将不得不改变用户界面,业务逻辑,数据库层和数据库 - 以及为那些在升级发生之前已经输入数据的用户决定你想为此提供的评级。可以想象,这导致了一个非常紧密耦合且难以维护的应用程序架构。



我们如何解决这个问题?好吧,如果我正在设计系统,我会看看使用查找表。换句话说,您可以将不同的问题存储为查找表中的条目,如下所示:

So, while that query would work, the question is why do I think the table is a poor design? Well, the biggest issue with it is that it's pretty inflexible. In other words, once you roll it out, you can't add new rating types without revisiting the whole application. Suppose you want to add a rating about the quality of social life; if you did this, you would have to change the UI, the business logic, the database layer and the database - as well as decide what rating you wanted to give for this for those users who had already entered their data before the upgrade takes place. As you can imagine, this leads to a very tightly coupled and difficult to maintain application architecture.

How do we solve this issue? Well, if I were designing the system I would look at using look up tables. In other words, you would store the different questions as entries in a lookup table that looked something like this:

QuestionID (Primary Key - Int)
Question (Int)

然后我会修改我的表,用户将存储他们的结果,以便它使用该表的外键,可能看起来像这样:

Then I would amend my table that the user will store their results in so that it used a foreign key to this table and possibly looked something like this:

ResultsID (PK INT)
QuestionID (Foreign Key pointing to the question table)
Rating (Int)

现在,虽然这是一个很好的第一步,但我们可以认为我们仍然有一个紧密约束的价值 - 让我们存储评级和它们在单独的查找表中的描述,并使此表中的Rating成为指向该单独的评级查找表的外键。我们现在有一组表可以用来驱动用户界面以便添加新的评级或问题只是意味着更新表格。



这个设计可能需要很多因此,例如,您可以使用标记标记的评级和描述查找表来指示该特定查找是否仍处于活动状态。通过这样做,您最终得到一个系统,您可以在不破坏参照完整性的情况下退出描述和评级。



我希望我已经设法给你一些食物想到这里,祝你好运。

Now, while this is a great first step, we could consider that we still have a tightly bound value in there - let's store the ratings and their description in a separate lookup table and have the Rating in this table become a foreign key pointing to that separate ratings lookup table. We now have a set of tables that we can use to drive the UI so that adding new ratings or questions simply means updating the table.

This design could be taken much further so, for instance, you could have the ratings and description lookup tables marked with a flag to indicate whether that particular lookup is still active. By doing this, you end up with a system where you can retire descriptions and ratings without breaking referential integrity.

I hope I've managed to give you some food for thought here, and good luck.


这篇关于将值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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