C#和Sql Server登录并返回数据 [英] C# and Sql server login and return data

查看:129
本文介绍了C#和Sql Server登录并返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的项目中有2个表单(登录表单,地址表单).

我需要在登录表(用户名,密码和权限)中返回SQL Server 2005中的数据.如何在表登录到表地址中返回结果.我使用类,但是当我调用类结果时,会得到空值
如果登录成功,我试图通过使用类完成地址表的结果,我必须显示地址表并关闭登录表.因此以地址表返回用户名和密码.所以我有这个类ConnectionClass
-------------------------------

登录表格


Hello all,


I have 2 forms in my project (Login form, address form).

I need to return my data in SQL server 2005, in login table (Username,Password and Permission). how can I return result in table login to table address. I use class, but when I call class result get me null values
I''m trying to accomplish result to address form by using class if the login is successful, I must show the address form and close the Login form.so return user name and password in address form. so I have this class ConnectionClass
-------------------------------

login form


connetionString = "Data Source=test;Initial Catalog=University;Integrated Security=True"; 
firstSql = "SELECT * FROM Login ";
connection = new SqlConnection(connetionString);
string userText = txtUsr.Text;
string passText = txtPassword.Text;

try {
 connection.Open();
command = new SqlCommand(firstSql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds, "First Table");
adapter.Dispose();
command.Dispose();
connection.Close();


for (j = 0; j <= ds.Tables[0].Rows.Count - 1; j++)
{
string s1 = Convert.ToString(ds.Tables[0].Rows[j].ItemArray[1].ToString());
string s2 = Convert.ToString(ds.Tables[0].Rows[j].ItemArray[2].ToString());
string s3 = Convert.ToString(ds.Tables[0].Rows[j].ItemArray[3].ToString());
if ((string)s1 == txtUsr.Text && (string)s2 == txtPassword.Text && (string)s3 == "admin") {

        ConnectionClass conobject = new ConnectionClass();
 s4 = (ds.Tables[0].Rows[j].ItemArray[4].ToString());
conobject.Setusernameprop(s1);
conobject.Setpasswordprop(s2);
conobject.Setlogidprop(s4);
addressform AdFor = new addressform ();
add.show();
}
else
{
}

catch (Exception ex)
{MessageBox.Show(ex.Message);}



-----------------
ConnectionClass代码



-----------------
ConnectionClass code

public class ConnectionClass
{
public  string Getusernameprop()
{return usernameprop;}

 public void Setusernameprop(string uName) 
{usernameprop = uName;}

 public string passwordprop = string.Empty;
 public string Getpasswordprop() 

{ return passwordprop;}


 public void Setpasswordprop(string pAssword)
{passwordprop = pAssword;}

public string logidprop = string.Empty;
public string Getlogidprop()
{return logidprop;}

 public void Setlogidprop(string iDLog)
{logidprop = iDLog;}
}

---------------------------------
地址表格

我与地址表链接

---------------------------------
address form

I link with address table

DBConnection abcd = new DBConnection();
MessageBox.Show(abcd .logidprop);


--------
在这种情况下,类返回null值


--------
in this case class return null value

推荐答案

你好,
按照下面的说明更改代码.


地址表格
Hello,
Change your code as per given below.


address form
public ConnectionClass abcd = new ConnectionClass();

public addressform ()
{
    InitializeComponent();
}

private void Form_Load(object sender, EventArgs e)
{
    MessageBox.Show(abcd.logidprop);
}



登录表格
旧代码



login form
Old code

addressform AdFor = new addressform ();
add.show();


新代码


New code

addressform AdFor = new addressform ();
adFor.abcd = conobject;
add.show();


这篇关于C#和Sql Server登录并返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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