我对 C# 真的很陌生,而且我没有编写此代码。我只是应该找出错误的原因。
我们有几台数控机床。数据库是 SQL Server 2005。
根据我阅读代码后的理解,整个代码块应该:
- 将数据库中的文件夹“推送”到作业排队的 CNC 机床。
- 一旦文件夹在系统上工作,就会对其进行修订。此修订版本应该是“从”存储此修订版本的位置,并将其复制到“拉到”目的地。
- 文件夹在存储一段时间后被删除。
“推送”文件夹的代码块有效。这告诉我没有连接错误。此外,SQL Server 连接具有 SSPI 的安全性。
我还可以登录数据库,这意味着我的 Windows 凭据没有问题。如果我去 SQL Server Management Studio 登录服务器,编写查询也适用于有问题的表。
但是当“拉”代码运行时,我收到以下错误:
选择 Distinct Resource, PullFrom, NML_MADL_XfriInstructions Where PullFrom <> 'na' Order By PullFrom, PullTo:
登录失败: 未知用户名或密码错误
这是代码:
public class frmNML_VantageMachinePrograms_Interface : Form
{
// Fields
[AccessedThroughProperty("lblProcessStep")]
private Label _lblProcessStep;
[AccessedThroughProperty("tbComments")]
private TextBox _tbComments;
[AccessedThroughProperty("Label4")]
private Label _Label4;
[AccessedThroughProperty("Label3")]
private Label _Label3;
[AccessedThroughProperty("Label1")]
private Label _Label1;
[AccessedThroughProperty("tbMaxAgeDays")]
private TextBox _tbMaxAgeDays;
[AccessedThroughProperty("bPerformProcess")]
private Button _bPerformProcess;
[AccessedThroughProperty("tbExcludeStepFlags")]
private TextBox _tbExcludeStepFlags;
[AccessedThroughProperty("tbResource")]
private TextBox _tbResource;
[AccessedThroughProperty("bClose")]
private Button _bClose;
[AccessedThroughProperty("Label2")]
private Label _Label2;
private IContainer components;
private SqlConnection oSqlServerVantageConnection;
private int IsAutoExecute;
private TextWriter swLogFile;
// Methods
public frmNML_VantageMachinePrograms_Interface()
{
base.Closing += new CancelEventHandler(this.frmNML_VantageMachinePrograms_Interface_Closing);
base.Load += new EventHandler(this.frmNML_VantageMachine_Interface_Load);
this.InitializeComponent();
}
private void bClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void bPerformProcess_Click(object sender, EventArgs e)
{
string prompt = "Push program files for parts associated with queued jobs\r\nRemove old program files\r\nPull revised program files\r\n\r\n";
prompt = !((StringType.StrCmp(this.tbResource.Text.Trim(), "", false) == 0) | (StringType.StrCmp(this.tbResource.Text.Trim(), "All", false) == 0)) ? (prompt + "for Resource " + this.tbResource.Text.Trim() + "?") : (prompt + "for all Resources?");
if (Interaction.MsgBox(prompt, MsgBoxStyle.Question | MsgBoxStyle.OKCancel, null) == MsgBoxResult.OK)
{
int num = IntegerType.FromString(this.tbExcludeStepFlags.Text);
this.LogComment(this.IsAutoExecute, "Begin Process");
if ((num & 1) == 0)
{
this.PushProgramFilesForQueuedJobs();
}
if ((num & 2) == 0)
{
this.RemoveOldProgramFiles();
}
if ((num & 4) == 0)
{
this.PullRevisedProgramFiles();
}
this.LogComment(this.IsAutoExecute, "Process Complete");
}
}
protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
}
private void frmNML_VantageMachine_Interface_Load(object sender, EventArgs e)
{
if (StringType.StrCmp(Interaction.Command(), "NoAutoExecute", false) == 0)
{
this.IsAutoExecute = 0;
}
else
{
this.IsAutoExecute = -1;
this.swLogFile = File.AppendText(@"\\Sidney9\NML_ServiceConnectData\NML_VantageMachinePrograms_Interface.log");
}
string connectionString = "Initial Catalog=MfgSys803; Data Source=SIDNEY9; Integrated Security=SSPI";
try
{
this.oSqlServerVantageConnection = new SqlConnection(connectionString);
this.oSqlServerVantageConnection.Open();
}
catch (Exception exception1)
{
Exception ex = exception1;
ProjectData.SetProjectError(ex);
Exception exception = ex;
this.LogComment(this.IsAutoExecute, "Error returned on SQL Server Connection (" + connectionString + ") - " + exception.Message);
if (this.IsAutoExecute != 0)
{
this.Close();
}
ProjectData.ClearProjectError();
}
if (this.IsAutoExecute != 0)
{
int num = IntegerType.FromString(this.tbExcludeStepFlags.Text);
this.LogComment(this.IsAutoExecute, "Begin Process");
if ((num & 1) == 0)
{
this.PushProgramFilesForQueuedJobs();
}
if ((num & 2) == 0)
{
this.RemoveOldProgramFiles();
}
if ((num & 4) == 0)
{
this.PullRevisedProgramFiles();
}
this.LogComment(this.IsAutoExecute, "Process Complete");
this.Close();
}
}
private void frmNML_VantageMachinePrograms_Interface_Closing(object sender, CancelEventArgs e)
{
this.oSqlServerVantageConnection.Close();
this.oSqlServerVantageConnection = null;
if (this.IsAutoExecute != 0)
{
this.swLogFile.Flush();
this.swLogFile.Close();
}
}
[DebuggerStepThrough]
private void InitializeComponent()
{
this.bPerformProcess = new Button();
this.bClose = new Button();
this.tbComments = new TextBox();
this.lblProcessStep = new Label();
this.Label1 = new Label();
this.tbResource = new TextBox();
this.Label2 = new Label();
this.tbMaxAgeDays = new TextBox();
this.Label3 = new Label();
this.Label4 = new Label();
this.tbExcludeStepFlags = new TextBox();
this.SuspendLayout();
this.bPerformProcess.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold, GraphicsUnit.Point, 0);
Point point = new Point(440, 0xe8);
this.bPerformProcess.Location = point;
this.bPerformProcess.Name = "bPerformProcess";
Size size = new Size(0x98, 0x20);
this.bPerformProcess.Size = size;
this.bPerformProcess.TabIndex = 4;
this.bPerformProcess.Text = "Perform Process";
this.bClose.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold, GraphicsUnit.Point, 0);
point = new Point(0x10, 0xe8);
this.bClose.Location = point;
this.bClose.Name = "bClose";
size = new Size(0x88, 0x20);
this.bClose.Size = size;
this.bClose.TabIndex = 5;
this.bClose.Text = "Close";
point = new Point(0x10, 0x40);
this.tbComments.Location = point;
this.tbComments.Multiline = true;
this.tbComments.Name = "tbComments";
this.tbComments.ScrollBars = ScrollBars.Vertical;
size = new Size(0x248, 0x98);
this.tbComments.Size = size;
this.tbComments.TabIndex = 6;
this.tbComments.Text = "";
this.lblProcessStep.BackColor = SystemColors.Highlight;
this.lblProcessStep.FlatStyle = FlatStyle.Popup;
this.lblProcessStep.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Italic | FontStyle.Bold, GraphicsUnit.Point, 0);
this.lblProcessStep.ForeColor = SystemColors.ActiveCaptionText;
point = new Point(0x10, 40);
this.lblProcessStep.Location = point;
this.lblProcessStep.Name = "lblProcessStep";
size = new Size(0x248, 0x18);
this.lblProcessStep.Size = size;
this.lblProcessStep.TabIndex = 7;
this.lblProcessStep.Text = "Process Comments";
this.lblProcessStep.TextAlign = ContentAlignment.MiddleCenter;
this.Label1.BackColor = SystemColors.Highlight;
this.Label1.FlatStyle = FlatStyle.Popup;
this.Label1.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Italic | FontStyle.Bold, GraphicsUnit.Point, 0);
this.Label1.ForeColor = SystemColors.ActiveCaptionText;
point = new Point(0x10, 8);
this.Label1.Location = point;
this.Label1.Name = "Label1";
size = new Size(160, 0x18);
this.Label1.Size = size;
this.Label1.TabIndex = 8;
this.Label1.Text = "Select Resource";
this.Label1.TextAlign = ContentAlignment.MiddleCenter;
this.tbResource.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Regular, GraphicsUnit.Point, 0);
point = new Point(0xb0, 8);
this.tbResource.Location = point;
this.tbResource.Name = "tbResource";
size = new Size(0x60, 0x17);
this.tbResource.Size = size;
this.tbResource.TabIndex = 9;
this.tbResource.Text = "All";
this.tbResource.TextAlign = HorizontalAlignment.Center;
point = new Point(160, 0xe0);
this.Label2.Location = point;
this.Label2.Name = "Label2";
size = new Size(0xa8, 0x10);
this.Label2.Size = size;
this.Label2.TabIndex = 10;
this.Label2.Text = "Remove Programs Older Than";
this.Label2.TextAlign = ContentAlignment.MiddleRight;
point = new Point(0x150, 0xe0);
this.tbMaxAgeDays.Location = point;
this.tbMaxAgeDays.Name = "tbMaxAgeDays";
size = new Size(0x20, 20);
this.tbMaxAgeDays.Size = size;
this.tbMaxAgeDays.TabIndex = 11;
this.tbMaxAgeDays.Text = "7";
this.tbMaxAgeDays.TextAlign = HorizontalAlignment.Center;
point = new Point(0x178, 0xe0);
this.Label3.Location = point;
this.Label3.Name = "Label3";
size = new Size(0x20, 0x10);
this.Label3.Size = size;
this.Label3.TabIndex = 12;
this.Label3.Text = "Days";
point = new Point(0xd8, 0x100);
this.Label4.Location = point;
this.Label4.Name = "Label4";
size = new Size(0x70, 0x10);
this.Label4.Size = size;
this.Label4.TabIndex = 14;
this.Label4.Text = "Exclude Step Flags:";
this.Label4.TextAlign = ContentAlignment.MiddleRight;
point = new Point(0x150, 0x100);
this.tbExcludeStepFlags.Location = point;
this.tbExcludeStepFlags.Name = "tbExcludeStepFlags";
size = new Size(0x20, 20);
this.tbExcludeStepFlags.Size = size;
this.tbExcludeStepFlags.TabIndex = 13;
this.tbExcludeStepFlags.Text = "0";
this.tbExcludeStepFlags.TextAlign = HorizontalAlignment.Center;
size = new Size(5, 13);
this.AutoScaleBaseSize = size;
size = new Size(0x268, 0x11e);
this.ClientSize = size;
this.Controls.Add(this.Label4);
this.Controls.Add(this.tbExcludeStepFlags);
this.Controls.Add(this.Label3);
this.Controls.Add(this.tbMaxAgeDays);
this.Controls.Add(this.Label2);
this.Controls.Add(this.tbResource);
this.Controls.Add(this.Label1);
this.Controls.Add(this.lblProcessStep);
this.Controls.Add(this.tbComments);
this.Controls.Add(this.bClose);
this.Controls.Add(this.bPerformProcess);
this.Name = "frmNML_VantageMachinePrograms_Interface";
this.Text = "NML Vantage - Machine Programs Interface";
this.ResumeLayout(false);
}
private void LogComment(int IsAutoExecute, string strComment)
{
if (IsAutoExecute != 0)
{
this.swLogFile.WriteLine(DateAndTime.Now.ToString() + " " + strComment);
}
else
{
if (this.tbComments.Text.Length > 0)
{
this.tbComments.Text = this.tbComments.Text + "\r\n";
}
this.tbComments.Text = this.tbComments.Text + strComment;
this.tbComments.Refresh();
}
}
[STAThread]
public static void Main()
{
Application.Run(new frmNML_VantageMachinePrograms_Interface());
}
private void PullRevisedProgramFiles()
{
int num = 0;
string sLeft = "";
string str2 = "Select Distinct Resource, PullFrom, PullTo From NML_MADL_XfrInstructions Where PullFrom<>'na'";
if ((StringType.StrCmp(this.tbResource.Text, "All", false) != 0) & (StringType.StrCmp(this.tbResource.Text, "", false) != 0))
{
str2 = str2 + " And Resource = '" + this.tbResource.Text + "'";
}
SqlCommand command = this.oSqlServerVantageConnection.CreateCommand();
command.CommandText = str2 + " Order By PullFrom, PullTo";
try
{
string str3;
string str5;
string str6;
int num2;
string[] strArray3;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
goto TR_0011;
}
TR_0004:
reader.Close();
goto TR_0002;
TR_0005:
num2++;
TR_000E:
while (true)
{
if (num2 >= strArray3.Length)
{
break;
}
string path = strArray3[num2];
num++;
string destFileName = str5 + @"\" + Path.GetFileName(path);
try
{
File.Copy(path, destFileName, true);
string[] strArray2 = new string[] { sLeft, "<tr><td align='center'>", str6, "</td><td align='left'>", Path.GetFileName(path), "</td><td align='left'>", str5, "</td><td align='left'>", str3 };
strArray2[9] = "</td></tr>";
sLeft = string.Concat(strArray2);
try
{
File.Delete(path);
}
catch (Exception exception1)
{
Exception ex = exception1;
ProjectData.SetProjectError(ex);
strArray2 = new string[] { "WARNING ... Error Encountered Deleted Revised File: ", path, " to: ", destFileName, ": ", ex.Message };
this.LogComment(this.IsAutoExecute, string.Concat(strArray2));
ProjectData.ClearProjectError();
}
}
catch (Exception exception4)
{
Exception ex = exception4;
ProjectData.SetProjectError(ex);
this.LogComment(this.IsAutoExecute, "WARNING ... Error Encountered Copying Revised File: " + path + " to: " + destFileName + ": " + ex.Message);
ProjectData.ClearProjectError();
}
goto TR_0005;
}
TR_0011:
while (true)
{
if (reader.Read())
{
str6 = StringType.FromObject(reader.GetValue(reader.GetOrdinal("Resource")));
str3 = StringType.FromObject(reader.GetValue(reader.GetOrdinal("PullFrom")));
str5 = StringType.FromObject(reader.GetValue(reader.GetOrdinal("PullTo")));
strArray3 = Directory.GetFiles(str3);
num2 = 0;
}
else
{
goto TR_0004;
}
break;
}
goto TR_000E;
}
catch (Exception exception5)
{
Exception ex = exception5;
ProjectData.SetProjectError(ex);
Exception exception3 = ex;
this.LogComment(this.IsAutoExecute, "WARNING ... Error Encountered Executing: " + str2 + ": " + exception3.Message);
ProjectData.ClearProjectError();
}
TR_0002:
if (StringType.StrCmp(sLeft, "", false) != 0)
{
this.SendProgramRetrievalNotification(sLeft);
}
this.LogComment(this.IsAutoExecute, "(3) Pull Revised Program Files Complete - " + num.ToString() + " Moved");
}
private void PushProgramFilesForQueuedJobs()
{
int num = 0;
string str = "Select Resource, MAFN, Version, PushTo From vNML_MAFN_CNCP_PushTo Where PushTo<>'na'";
if ((StringType.StrCmp(this.tbResource.Text, "All", false) != 0) & (StringType.StrCmp(this.tbResource.Text, "", false) != 0))
{
str = str + " And Resource = '" + this.tbResource.Text + "'";
}
SqlCommand command = this.oSqlServerVantageConnection.CreateCommand();
command.CommandText = str + " Order by Resource, MAFN";
try
{
string str3;
string str7;
int num4;
string[] strArray3;
SqlDataReader reader = command.ExecuteReader();
if (!reader.HasRows)
{
goto TR_0002;
}
else
{
str3 = @"\\Sidney2\AidLib\";
}
goto TR_000D;
TR_0002:
reader.Close();
goto TR_0000;
TR_0003:
num4++;
TR_000A:
while (true)
{
if (num4 >= strArray3.Length)
{
break;
}
string path = strArray3[num4];
num++;
string str6 = str7 + @"\" + Path.GetFileName(path);
if (!File.Exists(str6))
{
string str4;
try
{
str4 = "";
File.Copy(path, str6, true);
}
catch (Exception exception1)
{
Exception ex = exception1;
ProjectData.SetProjectError(ex);
Exception exception = ex;
str4 = " ERROR Encountered: " + exception.Message;
this.LogComment(this.IsAutoExecute, "WARNING ... Problem encountered copying file: " + path + " to: " + str6 + ": " + exception.Message);
ProjectData.ClearProjectError();
}
}
goto TR_0003;
}
TR_000D:
while (true)
{
if (reader.Read())
{
string path = str3 + IntegerType.FromObject(reader.GetValue(reader.GetOrdinal("MAFN"))).ToString("000000") + "." + IntegerType.FromObject(reader.GetValue(reader.GetOrdinal("Version"))).ToString("000");
string[] files = Directory.GetFiles(path);
str7 = StringType.FromObject(reader.GetValue(reader.GetOrdinal("PushTo")));
strArray3 = files;
num4 = 0;
}
else
{
goto TR_0002;
}
break;
}
goto TR_000A;
}
catch (Exception exception3)
{
Exception ex = exception3;
ProjectData.SetProjectError(ex);
Exception exception2 = ex;
this.LogComment(this.IsAutoExecute, "WARNING ... Error Encountered Executing: " + str + ": " + exception2.Message);
ProjectData.ClearProjectError();
}
TR_0000:
this.LogComment(this.IsAutoExecute, "(1) Push Program Files for Queued Jobs Complete - " + num.ToString() + " Copied");
}
private void RemoveOldProgramFiles()
{
int num = 0;
string str = "Select Distinct PushTo From NML_MADL_XfrInstructions Where PushTo<>'na'";
if ((StringType.StrCmp(this.tbResource.Text, "All", false) != 0) & (StringType.StrCmp(this.tbResource.Text, "", false) != 0))
{
str = str + " And Resource = '" + this.tbResource.Text + "'";
}
SqlCommand command = this.oSqlServerVantageConnection.CreateCommand();
command.CommandText = str + " Order By PushTo";
try
{
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
goto TR_0010;
}
TR_0002:
reader.Close();
goto TR_0000;
TR_0010:
while (reader.Read())
{
string path = StringType.FromObject(reader.GetValue(reader.GetOrdinal("PushTo")));
try
{
string[] files = Directory.GetFiles(path);
int index = 0;
goto TR_000B;
TR_0004:
index++;
TR_000B:
while (true)
{
if (index >= files.Length)
{
break;
}
string str3 = files[index];
DateTime creationTime = File.GetCreationTime(str3);
TimeSpan span = DateTime.Now.Subtract(creationTime);
if (span.Days > DoubleType.FromString(this.tbMaxAgeDays.Text))
{
try
{
File.Delete(str3);
num++;
}
catch (Exception exception1)
{
Exception ex = exception1;
ProjectData.SetProjectError(ex);
Exception exception = ex;
this.LogComment(this.IsAutoExecute, "WARNING ... Error Encountered Deleting File: " + str3 + ": " + exception.Message);
ProjectData.ClearProjectError();
}
}
goto TR_0004;
}
continue;
}
catch (Exception exception4)
{
Exception ex = exception4;
ProjectData.SetProjectError(ex);
Exception exception2 = ex;
this.LogComment(this.IsAutoExecute, "WARNING ... Error Encountered Checking for File Deletion from Folder: " + path + ": " + exception2.Message);
ProjectData.ClearProjectError();
}
}
goto TR_0002;
}
catch (Exception exception5)
{
Exception ex = exception5;
ProjectData.SetProjectError(ex);
Exception exception3 = ex;
this.LogComment(this.IsAutoExecute, "WARNING ... Error Encountered Executing: " + str + ": " + exception3.Message);
ProjectData.ClearProjectError();
}
TR_0000:
this.LogComment(this.IsAutoExecute, "(2) Remove Old Program Files Complete - " + num.ToString() + " Deleted");
}
private string SendProgramRetrievalNotification(string strHTML_FilesRetrieved)
{
string str2;
string str5 = "Sidney4";
MailMessage message = new MailMessage {
To = "NML_Programming_Notification@nmbc.com",
From = "NML_Programming_Notification@nmbc.com",
Subject = "Nicholson Mfg Ltd, Vantage Machine Programs Interface - Revised Programs Retrieved",
BodyFormat = MailFormat.Html,
Body = ((((("" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">" + "\r\n") + "<html>" + "<body>") + "<font face='Arial'>" + "<table width='100%' rules='all'>") + "<tr><td align='center'>Resource</td><td align='center'>File</td><td align='center'>To Folder</td><td align='center'>From Folder</td></tr>" + strHTML_FilesRetrieved) + "</table>" + "</font>") + "</body>" + "</html>"
};
SmtpMail.SmtpServer = str5;
try
{
SmtpMail.Send(message);
}
catch (Exception exception1)
{
string str4;
Exception ex = exception1;
ProjectData.SetProjectError(ex);
this.LogComment(this.IsAutoExecute, "WARNING ... Problem encountered sending notification to " + str4 + "\r\n" + "\r\n" + ex.Message);
Information.Err().Clear();
ProjectData.ClearProjectError();
}
finally
{
message = null;
}
return str2;
}
任何帮助表示赞赏。