c#.net怎么保存用户名和密码 c#.net怎么保存用户名和密码,点击保存密码下次登陆可以直接进去 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 详细一点的,cookies我不怎么会用 正解!所有的保存密码都是用的cookies 就是啊楼主你是要winform还是webform,winform就把登录名和密码写到数据库webform就写到cookies里面!cookies方面的这里也许有用http://www.bitscn.com/dotnet/c/200803/130948.html ........webfrom需要流winfrom需要数据库... winform可以建数据表来存储用户名和密码信息的表,然后建议把默认用户用配置文件来记录,在窗体Load时读出来并自动填入,用户登陆时回车就OK了。下面分别给两个代码吧。(一)保存用户名和密码到数据库记得在引用部分加上:using System.Data.SqlClient; private void button1_Click(object sender, EventArgs e) { string userName = textBox1.Text string userPswd = textBox2.Text string constr = "Server=(Local);User Id=Sa;Pwd=;DataBase=HSSN"; SqlConnection con = new SqlConnection(constr); con.Open(); string instr; instr = "insert into userinfo(name,pswd) values (" + "'" + userName + "'" + "," + "'" + userPswd + "'" + ")"; SqlCommand cmd = new SqlCommand(instr, con); try { cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } con.Close(); }(二)下面是一个Iin文件读写的类using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.IO;namespace IniOperator{ public class INIClass { public string inipath; [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); public INIClass(string INIPath) { inipath = INIPath; } /// /// 写入INI文件 /// /// 项目名称(如 [TypeName] ) /// 键 /// 值 public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.inipath); } /// /// 读出INI文件 /// /// 项目名称(如 [TypeName] ) /// 键 public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(500); int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath); return temp.ToString(); } /// /// 验证文件是否存在 /// /// 布尔值 public bool ExistINIFile() { return File.Exists(inipath); } } }(三、)读用上面的类读定Ini文件private INIClass inic;inic = new INIClass("写上你的文件名");//写入 if (inic.ExistINIFile()) { try { inic.IniWriteValue(textBox2.Text, textBox3.Text, textBox4.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } }//写入ini文件 try { textBox4.Text = inic.IniReadValue(textBox2.Text, textBox3.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } 如果是登陆的话,请把用户的个人设置序列化为文件(用户个人配置文件 - Profile) 请问C#怎样发送get winform 怎么跨项目调用窗口? 关键字高亮显示问题 一个关于相互调用、传值问题 .net winform程序:如何在多个窗体公用一个ContextMenuStrip? FTP服务器,非常急啊!!!!!!!!!急!急急!!急急急!!!急急急急!!!! 当页面回发服务器的时候 怎么记录当前页个空间的状态..? 高手请帮忙 显示图片的时候怎么一直提示“无效的参数” 急急急,请帮忙啊,有关于 comboBox control 的问题? 关于线程的问题 DataGridView控件怎么给DataGridViewComboBoxColumn赋值
webform就写到cookies里面!cookies方面的这里也许有用
http://www.bitscn.com/dotnet/c/200803/130948.html
然后建议把默认用户用配置文件来记录,在窗体Load时读出来并自动填入,用户登陆时回车就OK了。下面分别给两个代码吧。
(一)保存用户名和密码到数据库
记得在引用部分加上:
using System.Data.SqlClient;
private void button1_Click(object sender, EventArgs e)
{
string userName = textBox1.Text string userPswd = textBox2.Text string constr = "Server=(Local);User Id=Sa;Pwd=;DataBase=HSSN";
SqlConnection con = new SqlConnection(constr);
con.Open();
string instr;
instr = "insert into userinfo(name,pswd) values (" + "'" + userName + "'" + "," + "'" + userPswd + "'" + ")";
SqlCommand cmd = new SqlCommand(instr, con);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
} con.Close();
}(二)下面是一个Iin文件读写的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;namespace IniOperator
{
public class INIClass
{
public string inipath; [DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); public INIClass(string INIPath)
{
inipath = INIPath;
}
///
/// 写入INI文件
///
/// 项目名称(如 [TypeName] )
/// 键
/// 值
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.inipath);
}
///
/// 读出INI文件
///
/// 项目名称(如 [TypeName] )
/// 键
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
return temp.ToString();
}
///
/// 验证文件是否存在
///
/// 布尔值
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}
}
(三、)读用上面的类读定Ini文件
private INIClass inic;
inic = new INIClass("写上你的文件名");
//写入
if (inic.ExistINIFile())
{
try
{
inic.IniWriteValue(textBox2.Text, textBox3.Text, textBox4.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//写入ini文件
try
{
textBox4.Text = inic.IniReadValue(textBox2.Text, textBox3.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}