与C#相关的属性 [英] Properties related C#

查看:94
本文介绍了与C#相关的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据发送到电子邮件以及短信,现在我想要的是将值传播到每个班级

i已经设置了这样的属性

命名空间AVM_Technologies.MailClient

{

公共类MailClient

{



public string course

{

get;

set;

}

public string姓名

{

get;

set;



}

公共字符串电子邮件

{

get;设置;

}

公共字符串电话

{

get;设置;

}

公共字符串CompleteMail

{

get; set;

}

公共字符串主题

{get;组; }

}

}

当我试图在例如默认页面中为它们赋值时

MailClient.MailClient MailClient = new MailClient.MailClient();

MailClient.course = DropDownList1.SelectedItem.Text;

MailClient.name = reservation_Name.Text;

MailClient.Email = reservation_Email.Text;

MailClient.Phone = reservation_phone.Text;

//我还需要做的是在db中插入数据不同的类所以现在我这样做我有下面的插入类如下面

公共类UserDataInsertion

{

MailClient MailClient = new MailClient ();

public void MailDataSqlInsertion()

{

//将MailData插入DataBase

string Connectionstring = ConfigurationManager.ConnectionStrings [DataConnect]。连接字符串;

int Eval;

尝试

{

使用(SqlConnection con = new SqlConnection(Connectionstring))

{

con.Open();

String CmdText =插入dbo.MailTable_Data(名称,课程,电子邮件,电话,CompleteMail) )值('+ MailClient.Name +','+ MailClient.Course +','+ MailClient.Email +','+ MailClient.phone +','+ MailClient.CompleteMail + ';

SqlCommand SqlCmd = new SqlCommand(CmdText,con);

SqlCmd.ExecuteNonQuery();

con.Close() ;

}

}

catch

{







}

}

}

访问MailCientData在MailDataSqlInsertion中( )方法,但属性为空或无请请关注此事...提前感谢..



我尝试过:



我已经让对象访问我在insert命令中发送的属性来存储数据但是它的null可以提供KT吗?

I need to send the Data to Email as well as SMS and now what i want is to circulate the values to every class
i have set the properties like this
namespace AVM_Technologies.MailClient
{
public class MailClient
{

public string Course
{
get ;
set;
}
public string Name
{
get ;
set ;

}
public string Email
{
get; set;
}
public string Phone
{
get; set;
}
public string CompleteMail
{
get;set;
}
public string Subject
{ get; set; }
}
}
and when i tried to assign them value in e.g in default page like this
MailClient.MailClient MailClient = new MailClient.MailClient();
MailClient.course = DropDownList1.SelectedItem.Text;
MailClient.name = reservation_Name.Text;
MailClient.Email = reservation_Email.Text;
MailClient.Phone = reservation_phone.Text;
// also what i need to do is insert data in db from different class so now im doing it like this I have sepreate class for insertion like below
public class UserDataInsertion
{
MailClient MailClient = new MailClient();
public void MailDataSqlInsertion()
{
// Insertion of MailData to DataBase
string Connectionstring = ConfigurationManager.ConnectionStrings["DataConnect"].ConnectionString;
int Eval;
try
{
using (SqlConnection con = new SqlConnection(Connectionstring))
{
con.Open();
String CmdText = "insert into dbo.MailTable_Data(Name,Course,Email,Phone,CompleteMail) Values('" + MailClient.Name + "','" + MailClient.Course + "','" + MailClient.Email + "','" + MailClient.phone + "','" + MailClient.CompleteMail + "'";
SqlCommand SqlCmd = new SqlCommand(CmdText, con);
SqlCmd.ExecuteNonQuery();
con.Close();
}
}
catch
{



}
}
}
am accessing the MailCientData in MailDataSqlInsertion() method but properties are empty or null please breif me about this ... thanks in advance ..

What I have tried:

I have made object to access the properties that im sending in insert command to store data but its null can you provide KT about it??

推荐答案

问题是您没有在 MailDataSqlInsertion MailClient 的现有实例c $ c>方法,但是一个新的空方法:

The problem is that you don't use an existing instance of a MailClient in your MailDataSqlInsertion method, but a new, empty one:
MailClient MailClient = new MailClient();
public void MailDataSqlInsertion()
    {
    ...
    String CmdText = "insert into dbo.MailTable_Data(Name,Course,Email,Phone,CompleteMail) Values('" + MailClient.Name + "','" + MailClient.Course + "','" + MailClient.Email + "','" + MailClient.phone + "','" +

你不想在这里创建新的winstance,而是使用传递给你的那个。

有两种方法可以做到这一点:

1)使 MailClient 实例成为 MailDataSqlInsertion 方法,因此您每次都必须传递数据你使用它。

2)使 UserDataInsertion 类构造函数需要一个 MailClient 参数和保存它以便以后需要。



第一个选项是最灵活的,因为你可以重用 UserDataInsertion 连续几个消息的实例。



但无论你做什么,都不要像那样做数据库工作!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

You new to not create ne winstance here, but use the one that is passed to you.
There are two ways to do this:
1) Make the MailClient instance a parameter to the MailDataSqlInsertion method, so you have to pass the data each time you use it.
2) Make the UserDataInsertion class constructor require a MailClient parameter and save it for when it's needed later.

The first option is the most flexible, as you can reuse the UserDataInsertion instance for several messages in succession.

But whatever you do, don't do database work like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


这篇关于与C#相关的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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