我目前在使用CSV到SQL转换器时遇到了一些问题。该程序用于创建命令以编辑数据库用户列表更新或添加新列表。我的问题是,如果用户没有在CSV文件中输入密码,我希望将其设置为用户ID。我当前有这个IF语句试图检测字符串是否为空但它总是说该字符串是空的,并给我设置弹出框,即使我在单元格中有文本。
if (password != null)
{
System.Windows.Forms.MessageBox.Show("Setting Password to User ID");
}
我也尝试过password = password.replace(userid),但它不会改变字符串。如果有帮助,我已将我的整个代码包括在内。
private void LoadBtn_Click(object sender, EventArgs e)
{
//Opens a browse box to allow the user to select which file, only CSV's allow allowed
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "CSV Files (*.csv)|*.csv";
openFileDialog1.FilterIndex = 1;
//empties text box when clicked | loads file location and name to load directory text box at top
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
ConvertedText.Text = string.Empty;
LoadDirectory.Text = openFileDialog1.FileName.ToString();
}
string filename = LoadDirectory.Text;
string[] Lines = File.ReadAllLines(filename);
string[] Fields;
for (int i = 1; i < Lines.Length; i++)
{
string outfile = "";
Fields = Lines[i].Split(new char[] { ',' });
string userid = Fields[0];
string password = Fields[1];
string fullname = Fields[2];
string address = Fields[3];
string telephone = Fields[4];
string email = Fields[5];
string role = Fields[6];
string department = Fields[7];
if (password != null)
{
System.Windows.Forms.MessageBox.Show("Setting Password to User ID");
}
outfile += "IF exists (SELECT USERID FROM WUSERS WHERE USERID='" + userid + "')" + Environment.NewLine;
outfile += "begin" + Environment.NewLine;
outfile += Environment.NewLine;
outfile += "-- Update it" + Environment.NewLine;
outfile += "print 'updated'" + Environment.NewLine;
outfile += "update WUSERS set FULLNAME= '" + fullname + "' where userid= '" + userid + "'" + Environment.NewLine;
outfile += "update WUSERS set PW= '" + password + "' where userid= '" + userid + "'" + Environment.NewLine;
outfile += "update WUSERS set addr= '" + address + "' where userid= '" + userid + "'" + Environment.NewLine;
outfile += "update WUSERS set telno= '" + telephone + "' where userid= '" + userid + "'" + Environment.NewLine;
outfile += "update WUSERS set email= '" + email + "' where userid= '" + userid + "'" + Environment.NewLine;
outfile += "update WUSERS set ROLEID= '" + role + "' where userid= '" + userid + "'" + Environment.NewLine;
outfile += "update WUSERS set dept= '" + department + "' where userid= '" + userid + "'" + Environment.NewLine;
outfile += "end" + Environment.NewLine;
outfile += "else" + Environment.NewLine;
outfile += "begin" + Environment.NewLine;
outfile += "-- add it" + Environment.NewLine;
outfile += "print 'added'" + Environment.NewLine;
outfile += "insert into WUSERS (USERID,PW,FULLNAME,ADDR,TELNO,EMAIL,ROLEID,DEPT) Values ('" + userid + "','" + password + "','" + fullname + "','" + address + "','" + telephone + "','" + email + "','" + role + "','" + department + "')";
outfile += Environment.NewLine;
outfile += "end";
outfile += Environment.NewLine;
outfile += Environment.NewLine;
outfile += Environment.NewLine;
outfile += "-----------------------------------------------------------------------------------------------------------------------------------------------";
outfile += Environment.NewLine;
outfile += Environment.NewLine;
outfile += Environment.NewLine;
ConvertedText.AppendText(outfile);
}
}
提前感谢您的任何意见。
欧文