帮助我将asp.net与数据库连接 [英] help me to connect asp.net with database

查看:52
本文介绍了帮助我将asp.net与数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中设计了一个简单的首页

我已经创建了一个名为Reg的数据库,该数据库使用此表单中的值

它是一个简单的注册表格

代码:

 <   html  > ; 
<  头部 > 
<      ="   GENERATOR" 内容   Microsoft FrontPage 5.0 " <      ="   ProgId" 内容   FrontPage.Editor .Document" <      ="   Content-Type"   内容  ="  text/html; charset = windows-1252" <  标题 > 新页1 <  /title  > 
<  /head  > 
<  正文 > 
<  表单    ="   frmReg" 方法   POST"   动作  =" -WEBBOT-SELF-" <!-  网络机器人bot ="SaveResults" u-file ="C:\ Documents and Settings \ Administrator \ Desktop \ reg \ _private \ form_results.csv"s-format =" TEXT/CSV"s-label-fields =" TRUE" ->   <    class    style1 " <   tr  > 
                <   td  > 
                    名称:<  /td  > 
                <   td  > 
                    <   asp:TextBox     ID   ="   runat   服务器" <  输入    ="  文本" 名称   T1"   大小  ="  20" <  /asp:TextBox  >  <  /td  > 
                    <   asp:RangeValidator     ControlToValidate   ="   MinimumValue    A "  MaximumValue    Z" 类型  文本"  EnableClientScript   ="    文本  ="    运行  ="    / > 

            <  /tr  > 
            <   tr  > 
                <   td  > 
                    地址:<  /td  > 
                <   td  > 
                    <   textarea     ="   2" 名称   S1"    cols   ="  20" <  /textarea  >  <  > 
<   asp:RangeValidator     ControlToValidate   ="   MinimumValue    A ,a"  MaximumValue    Z,z" 类型  ="     ="   false" 文本  只有字母!"  runat   ="    / > 

            <  /tr  > 
            <   tr  > 
                <   td  > 
                   电子邮件:<  /td  > 
                <   td  > 
            <   asp:TextBox     ID   ="   runat   服务器" <  输入    ="  文本" 名称   T3"   大小  ="  20" <  /asp:TextBox  >  <  /td  > 
<   asp:RangeValidator     ControlToValidate   ="   MinimumValue    A ,a"  MaximumValue    Z,z" 类型  ="     ="   false" 文本  只有字母!"  runat   ="    / > 

            <  /tr  > 
            <   tr  > 
                <   td  > 
                    密码:<  /td  > 

                <   td  > 
            <   asp:TextBox     ID   ="   runat   服务器"  TextMode   密码" <  输入    ="  文本" 名称   T4"   大小  ="  20" <  /asp:TextBox  >  <  /td  > 
            <  /tr  > 
            <   tr  > 
                <   td  > 
                    年龄:<  /td  > 
                <   td  > 
                    <   asp:TextBox     ID   ="   runat   服务器" <  输入    ="  文本" 名称  > T5"    大小  ="  20" <  /asp:TextBox  >  <  /td  > 
<   asp:RangeValidator     ControlToValidate   ="   MinimumValue    0 "  MaximumValue    9" 类型  数字"  EnableClientScript   ="    文本  只有数字!" 短跑  ="    / > 

            <  /tr  > 
            <   tr  > 
                <   td  > 
                    性别:<  /td  > 
                <   td  > 
                <  选择    ="   1" 名称   D1" > 
                <  选项 > 男性<  /option  > 
                <  选项 > 女性<  /option  > 
                <  /select  >  <  /td  > 
            <  /tr  > 
        <  /table  > 
    <   p  >  <  输入    ="  提交"   ="    名称  ="     onclick   =   Submit() >  ><  输入    ="  重置"   ="    名称  ="  B2" <  > 
<  /form  > 
<  /body  > 
<  /html  >  



Jst编写用于连接数据库和
的代码 解决方案

使用LINQ实现数据将显示为

 使用(SomeDataContext db =  SomeDataContext())
{
 var  newUser =  UserBO {Username = "  John",电子邮件=   john@gmail.com "};
db.Users.InsertOnSubmit(newUser);
db.SubmitChanges();
} 



其中SomeDataContext-正在创建的DataContext对象,将表USERS从Server Explorer拖到.dbml(LINQ to SQL类)文件中


 string conString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];  //this is the connection string defined in web.config

SqlConnection sqlConn = new SqlConnection(conString);
sqlConn.Open();
SqlCommand cmd= new SqlCommand("insert into table_name values(''"+TxtName.Text+"'',''"+TxtAddress.Text+"'''',"+TxtEmail.Text+"'',''"+TxtPassword.Text+"'',''"+TxtAge.Text+"'',''"+D1.SelectedItem.Text+"''", sqlConn);//The sequence of fields may vary as per the sequence in your DB table

cmd.ExecuteNonQuery();// this returns values greater than 1 if values are sucessfully inserted in DB



上面的代码将帮助您在db表中插入值.

上面的代码只是一个示例,请确保根据您的要求进行更改.


当前正在使用Microsoft FRONTPAGE.

我该在上面的代码上面写这段代码吗?

请指定所需的更改.......


谢谢....


I have designed a simple front page in asp.net

I have already created a databse named Reg which takes the values from this form

Its a simple registration form

Code:

<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<form name="frmReg" method="POST" action="--WEBBOT-SELF--">
  <!--webbot bot="SaveResults" u-file="C:\Documents and Settings\Administrator\Desktop\reg\_private\form_results.csv" s-format="TEXT/CSV" s-label-fields="TRUE" --><table class="style1">
           <tr>
                <td>
                    Name:</td>
                <td>
                    <asp:TextBox ID="TxtName" runat="server">
                    <input type="text" name="T1" size="20"></asp:TextBox></td>
                    <asp:RangeValidator ControlToValidate="TxtName" MinimumValue="A" MaximumValue="Z" Type="Text" EnableClientScript="false" Text="Only Alphabets!" runat="server" />

            </tr>
            <tr>
                <td>
                    Address:</td>
                <td>
                    <textarea rows="2" name="S1" cols="20"></textarea></td>
<asp:RangeValidator ControlToValidate="TxtAddress" MinimumValue="A,a" MaximumValue="Z,z" Type="Text" EnableClientScript="false" Text="Only Alphabets!" runat="server" />

            </tr>
            <tr>
                <td>
                   Email:</td>
                <td>
            <asp:TextBox ID="Txtemail" runat="server">
            <input type="text" name="T3" size="20"></asp:TextBox></td>
<asp:RangeValidator ControlToValidate="Txtemail" MinimumValue="A,a" MaximumValue="Z,z" Type="Text" EnableClientScript="false" Text="Only Alphabets!" runat="server" />

            </tr>
            <tr>
                <td>
                    Password:</td>

                <td>
            <asp:TextBox ID="TxtPassword" runat="server" TextMode="Password">
            <input type="text" name="T4" size="20"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    Age:</td>
                <td>
                    <asp:TextBox ID="TxtAge" runat="server">
                    <input type="text" name="T5" size="20"></asp:TextBox></td>
<asp:RangeValidator ControlToValidate="TxtAge" MinimumValue="0" MaximumValue="9" Type="number" EnableClientScript="false" Text="Only number!" runat="server" />

            </tr>
            <tr>
                <td>
                    Gender:</td>
                <td>
                <select size="1" name="D1">
                <option>Male</option>
                <option>Female</option>
                </select></td>
            </tr>
        </table>
    <p><input type="submit" value="Submit" name="B1" onclick=Submit()><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>



Jst write the code for connecting to database and
inserting data into database on Submit.

解决方案

To implement it using LINQ will shows like

using(SomeDataContext db=new SomeDataContext())
{ 
var newUser=new UserBO{Username="John", Email="john@gmail.com"};
db.Users.InsertOnSubmit(newUser);
db.SubmitChanges();
}



where SomeDataContext - DataContext object that is creating where you drag the table USERS from Server explorer to your .dbml (LINQ to SQL class) file


 string conString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];  //this is the connection string defined in web.config

SqlConnection sqlConn = new SqlConnection(conString);
sqlConn.Open();
SqlCommand cmd= new SqlCommand("insert into table_name values(''"+TxtName.Text+"'',''"+TxtAddress.Text+"'''',"+TxtEmail.Text+"'',''"+TxtPassword.Text+"'',''"+TxtAge.Text+"'',''"+D1.SelectedItem.Text+"''", sqlConn);//The sequence of fields may vary as per the sequence in your DB table

cmd.ExecuteNonQuery();// this returns values greater than 1 if values are sucessfully inserted in DB



The above code will help you insert values in the db table.

The above code is just an example , make sure that you make the changes in it as per your requirements.


i m currently using Microsoft FRONTPAGE.

Shud i write this code above the previous code?

Please specify the changes required.......


Thanks....


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

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