如何将aspx代码转换为ado .net [英] How to covert aspx code in to ado .net

查看:92
本文介绍了如何将aspx代码转换为ado .net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 System.Data;
使用 System.Net.Mail;

protected void btnPass_Click( object sender,EventArgs e)
{
// 创建连接字符串和SQL声明
string strConnection = ConfigurationManager.ConnectionStrings [ ConnectionString的]的ConnectionString。
string strSelect = SELECT UserName,Password FROM Users WHERE Email = @Email;

SqlConnection connection = new SqlConnection(strConnection);
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = strSelect;

SqlParameter email = new SqlParameter( @ Email,SqlDbType.VarChar, 50 );
email.Value = txtEmail.Text.Trim()。ToString();
command.Parameters.Add(email);

// 创建数据集以存储结果,使用DataAdapter填充数据集
DataSet dsPwd = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter(command);
connection.Open();
dAdapter.Fill(dsPwd);
connection.Close();
if (dsPwd.Tables [ 0 ]。Rows.Count > 0
{
MailMessage loginInfo = new MailMessage();
loginInfo.To.Add(txtEmail.Text.ToString());
loginInfo.From = new MailAddress( YourID@gmail.com);
loginInfo.Subject = 忘记密码信息;

loginInfo.Body = 用户名: + dsPwd.Tables [< span class =code-digit> 0
]。行[ 0 ] [ UserName] + < br>< br> ;密码: + dsPwd.Tables [ 0 ]。行[ 0 ] [< span class =code-string>
密码] + <峰; br><峰; br>中;
loginInfo.IsBodyHtml = true ;
SmtpClient smtp = new SmtpClient();
smtp.Host = smtp.gmail.com;
smtp.Port = 587 ;
smtp.EnableSsl = true ;
smtp.Credentials = new System.Net.NetworkCredential( YourGmailID@gmail.com YourGmailPassword);
smtp.Send(loginInfo);
lblMessage.Text = 密码被发送到您的电子邮件ID,您现在可以< a href = Login.aspx >登录< / a>;
}
else
{
lblMessage.Text = 电子邮件地址未注册;
}

}





我尝试过: < br $>


i不明白代码转换为ado .net ..

解决方案

ADO.Net提供集合用于访问SQL等数据源的类。您已经在使用ADO.Net,因为诸如SqlConnection,SqlCommand,SqlParameter等类是ADO.Net的一部分。您不需要转换它。

在上面的代码中,SQLConnection允许您连接到数据库,SQLCommand允许您运行变量strSelect中的命令。通过对数据库运行查询返回的结果填充到数据集dsPWD中,这是表的内存表示。



通过googling进一步了解ado.net它

using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Net.Mail;

protected void btnPass_Click(object sender, EventArgs e)
    {
        //Create Connection String And SQL Statement
        string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string strSelect = "SELECT UserName,Password FROM Users WHERE Email = @Email";

        SqlConnection connection = new SqlConnection(strConnection);
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = strSelect;

        SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
        email.Value = txtEmail.Text.Trim().ToString();
        command.Parameters.Add(email);

        //Create Dataset to store results and DataAdapter to fill Dataset 
        DataSet dsPwd = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter(command);
        connection.Open();
        dAdapter.Fill(dsPwd);
        connection.Close();
        if(dsPwd.Tables[0].Rows.Count > 0 )
        {
            MailMessage loginInfo = new MailMessage();
            loginInfo.To.Add(txtEmail.Text.ToString());
            loginInfo.From = new MailAddress("YourID@gmail.com");
            loginInfo.Subject = "Forgot Password Information";

            loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "<br><br>Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br><br>";
            loginInfo.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; 
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("YourGmailID@gmail.com", "YourGmailPassword");
            smtp.Send(loginInfo);
            lblMessage.Text = "Password is sent to you email id,you can now <a href="Login.aspx">Login</a>";
        }
        else
        {
            lblMessage.Text = "Email Address Not Registered";
        }
        
    }



What I have tried:

i dont understand the code plase convert in to ado .net ..

解决方案

ADO.Net provide set of classes that are used to access the data sources such as SQL. You are already using ADO.Net as classes such as SqlConnection ,SqlCommand ,SqlParameter etc are part of ADO.Net. You don't need to convert it.
In the above code SQLConnection allows you to connect to database, and SQLCommand allows you to run the command which is in the variable strSelect. The result returned by running the query against the database is filled into dataset dsPWD, which is in-memory representation of table.

Read further about the ado.net by googling it.


这篇关于如何将aspx代码转换为ado .net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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