所以我在Visual Studio C#中创建个人项目
我对C#和编码很新。我创建了一个登录面板,我想要一种存储数据的方法,而不需要数据库服务器,更像是文本文件。
如图所示存储
--------------------
Profiles
[Username] [Password]
Admin 123abc
Test imboss
--------------------
并且毕竟它有一个检查方法来查看是否 usernameTextBox和passwordTextBox等于任何配置文件。
private void loginButton_Click(object sender, EventArgs e)
{
string username, password;
username = textBox1.Text;
password = textBox2.Text;
if ("userProfileName".ToString() == username && "userPassword".ToString() == password)
{
//login
}
else
{
MessageBox.Show("You have entered a wrong user name or password!");
}
}
答案 0 :(得分:1)
一种简单的方法是将用户名和密码存储在app.config文件中。
var userPassword = ConfigurationManager.AppSettings[username];
if (userPassword == password)
{
MessageBox.Show("Welcome");
} else {
MessageBox.Show("You have entered a wrong user name or password!");
}
可以使用ConfigurationManger类从文件中读取值。要使用该类,您需要在文件顶部添加引用和使用语句。
import React, { Component } from 'react';
import {
StyleSheet,
View,
Switch,
ListView
} from 'react-native';
export default class FriendListBody extends Component {
ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
constructor(props) {
super(props);
let friends = this.props.friends.map((friend) => {
return {
...friend,
selected: false
}
});
this.state = {
datasource: this.ds.cloneWithRows(friends),
friends
};
this._renderRow= this._renderRow.bind(this);
this._setValue = this._setValue.bind(this);
}
render() {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.datasource}
renderRow={this._renderRow}
style={styles.listView}
enableEmptySections={true}
/>
</View>
);
}
_setValue(id, value) {
let newList = this.state.friends.slice();
let pos = -1;
for (let i = 0; i < this.state.friends.length; i++) {
if (id === this.state.friends[i]._id) {
pos = i;
break;
}
}
newList[pos].selected = value;
const datasource = this.ds.cloneWithRows(newList);
this.setState({
friends: newList,
datasource: datasource
);
}
_renderRow(rowData) {
return (
<View key={rowData._id} style={{ borderRadius: 10 }}>
<Switch
onValueChange={(value) => this._setValue(rowData._id, value)}
style={{ marginBottom: 10 }}
value={ rowData.selected } />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#d9d9d9',
},
listView: {
flex: 1,
borderColor: 'grey'
}
});
答案 1 :(得分:-1)
using System.Data;
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection("Data Source=IP; Initial Catalog=DataBaseName; User Id=sa; password=123;");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Profiles WHERE USERNAME=@USERNAME AND PASSWORD=@PASSWORD", conn);
cmd.Parameters.AddWithValue("@USERNAME",textBox1.Text);
cmd.Parameters.AddWithValue("@PASSWORD",textBox2.Text);
if(cmd.ExecuteNonQuery()>0)
{
//Login
}
else
{
MessageBox.Show("You have entered a wrong user name or password!");
}
conn.Close();