用于将表单数据插入Microsoft SQL数据库的代码 [英] Code for inserting form data to Microsoft SQL database

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

问题描述

我创建了一个.aspx页面.它包含某种形式(例如注册页面形式).我还创建了一个.cs(当我们单击提交"按钮时将激活).现在如何编写代码在C#中创建与我系统上安装的Microsoft SQL Server 2005 的连接.我还想知道用于将表单数据(在.aspx页中)插入Microsoft SQL Server数据库的代码. :sigh:

Hi I have created a .aspx page.It contain some form(like registration page form).I have also created a .cs(which will activate when we click submit button).Now how to write the code in c# to create connection with Microsoft SQL Server 2005 which is installed on my system.I also want to know the code for inserting the form data(in .aspx page) to Microsoft SQL Server database. :sigh:

推荐答案

如果您能够使用Ado.net实体框架

使用Entities对象将数据保存到Sql中很容易
在这里,您将获得User表名,因为Class为该表创建了一个对象,并将数据插入其中


If u are able to use Ado.net Entity Frame Work

It''s easy to save the data into Sql by using Entities object
here u will get the User table name as Class create a object for that and insert the data into it


try
        {
            User u = new User();

            if (checkbox1.Checked == true)
            {
                u.FirstName = txtFirstName.Text.ToString();
                u.LastName = txtSurName.Text.ToString();
                u.DateOfBirth = Convert.ToDateTime(txtDOB.Text);
                u.MobileNo = txtphone.Text.ToString();
                u.UserId = txtuserid.Text.ToString();
                u.EmailId = txtemail.Text.ToString();
                u.CityId = Convert.ToInt32(1);

                u.UserId = txtuserid.Text.ToString();
                if (radbtn.Checked == true)
                {
                    u.Gender = "Male";
                }
                else if (radbtn1.Checked == true)
                {
                    u.Gender = "Female";
                }
                o1.AddToUsers(u);
                //o1.AcceptAllChanges();
                o1.SaveChanges();

                //Response.Write("<script>alert('Record  inserted ')</script>");
                Label1.Visible = true;
            }
            else
            {
                //Response.Write("<script>alert('Agree the terms and conditions')</script>");
                Label1.Visible = true;
                Label1.Text = "Record cannot be Inserted";
            }
      catch (Exception ce)
      {
          string c = ce.Message;
      }


看起来您是.Net编程的新手.

Looks like you are new to .Net programming.


String strSQL;
SqlCommand sqlCmd;
SqlConnection sqlConn;
strSQL = "INSERT INTO MyTable (Col1, Col2) VALUES........."
sqlConn = New SqlConnection("........")
sqlCmd = New SqlCommand(strSQL, sqlConn)

sqlConn.Open()
sqlCmd.ExecuteNonQuery()
sqlConn.Close()



寻求帮助

用谷歌搜索或访问msdn,您肯定会找到答案.

如有疑问,请访问印地文论坛.很多人在那里帮助你.... :)

http://www.codeproject.com/Forums/1580229/Hindi.aspx:)



See for help

Google it or go to msdn you definitely find an answer.

If you have queries please visit hindi forum. Lots of guys there to help u.... :)

http://www.codeproject.com/Forums/1580229/Hindi.aspx :)


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

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