实例介绍
【实例简介】
利用神经网络算法来计算 水质
根据标准分出等级
如果 没有安装access驱动可参考这里
1.下载2010 Office system 驱动程序
这里 https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13255
2.安装AccessDatabaseEngine.exe 我选择中文 64位,根据自己机器情况选择即可
【实例截图】
【核心代码】
namespace 神经网络
{
class DataAccess
{
//private static string ConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" Application.StartupPath "\\数据.mdb";//连接数据库字符串
private static string ConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" Application.StartupPath "\\数据.mdb";//Persist Security Info=False;
public static DataTable GetDataSetByTableName(string table)
{
using (OleDbConnection con = new OleDbConnection(ConnectString))
{
string sql = "select * from" table;
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
DataSet ds = new DataSet();
adapter.Fill(ds, table);
return ds.Tables[0];
}
catch (OleDbException ex)
{
throw new Exception(ex.Message);
}
}
}
public static DataSet GetDataSetBySql(string sql)
{
using (OleDbConnection con = new OleDbConnection(ConnectString))
{
OleDbDataAdapter ad = new OleDbDataAdapter(sql, ConnectString);
DataSet ds = new DataSet();
try
{
ad.Fill(ds);
return ds;
}
catch (OleDbException e)
{
throw new Exception(e.Message);
}
}
}
public static DataSet GetDataSetByOleDb(string sql)
{
using (OleDbConnection con = new OleDbConnection(ConnectString))
{
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
DataSet ds = new DataSet();
// try
// {
adapter.Fill(ds);
return ds;
// }
// catch (OleDbException ex)
//{
// throw new Exception(ex.Message);
// }
}
}
public static bool UpdateDataTable(string sql)
{
using (OleDbConnection con = new OleDbConnection(ConnectString))
{
try
{
con.Open();
OleDbCommand comm = new OleDbCommand(sql, con);
if (comm.ExecuteNonQuery() > 0)
{
return true;
}
else
{
return false;
}
}
catch (OleDbException ex)
{
throw new Exception(ex.Message);
}
}
}
public static void UpdateDataSet(DataSet ds, string sql)
{
using (OleDbConnection con = new OleDbConnection(ConnectString))
{
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
adapter.Update(ds, "table");
}
catch (OleDbException ex)
{
throw new Exception(ex.Message);
}
}
}
}
}
主页面类
public bool StudyData_input(int INN)
{
bool functionReturnValue = false;
int i = 0;
int j = 0;
double pre_times = 0;
double phanum = 0;
double Input = 0;
double pre_error = 0;
double alpha = 0;
double beta = 0;
try
{
pre_error = Convert.ToDouble(txtb_Wucha.Text.Trim());
alpha = Convert.ToDouble(txtb_shuchuyinghan.Text.Trim());
beta = Convert.ToDouble(txtb_yinghanshuchu.Text.Trim());
pre_times = Convert.ToDouble(txtb_studytimes.Text.Trim());
phanum = Convert.ToDouble(txtb_Yinggeshu.Text.Trim());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);//后面两个参数分别是 按钮类型和图标类型。
functionReturnValue = false;
return functionReturnValue;
}
System.IO.FileStream fs = new System.IO.FileStream(Application.StartupPath "\\训练样本.txt", FileMode.Create);
StreamWriter sr = new StreamWriter(fs, Encoding.Default);
sr.Write(alpha);
sr.Write(" ");
sr.Write(beta);
sr.Write(" ");
sr.Write(pre_error);
sr.Write(" ");
sr.Write(pre_times);
sr.Write(" ");
sr.Write(phanum);
sr.Write(" ");
sr.Write(INN);
try
{
DataSet ds = DataAccess.GetDataSetByOleDb("select 等级," choice ",等级值 from study");
this.dataGridView1.DataSource = ds.Tables[0];
for (j = 0; j <= ds.Tables[0].Rows.Count - 1; j )
{
if ((j < 5))
{
for (i = 0; i <= INN - 1; i )
{
// if (ds.Tables[0].Rows[i][j] == DBNull.Value)
// {
// Input = 0;
// }
// else {
//MessageBox.Show(Convert.ToDouble(ds.Tables[0].Rows[j][i 1]).ToString());
Input = Convert.ToDouble(ds.Tables[0].Rows[j][i 1]);
// }
sr.Write(" ");
sr.Write(Input);
}
}
else
{
for (i = 0; i <= INN - 1; i )
{
biaozhun[j - 5, i] = Convert.ToDouble(ds.Tables[0].Rows[j][i 1]);//biaozhun(j - 5, i) = DsS.Tables.Item(0).Rows[i 1);
}
}
}
sr.Close();
fs.Close();
functionReturnValue = true;
}
catch (OleDbException ex)
{
MessageBox.Show(ex.ToString());
functionReturnValue = false;
}
return functionReturnValue;
}
public void Test_DataInput(int INN)
{
int i = 0;
int j = 0;
System.IO.FileStream fs = new System.IO.FileStream(Application.StartupPath "\\测试样本.txt", FileMode.Create);
StreamWriter sr = new StreamWriter(fs, Encoding.Default);
double[] Input = new double[40];
string dbnulltype = "";
int length = 0;
try
{
string sql = " select 年份,点号,水期,含水层," choice " from water ";
DataSet ds = DataAccess.GetDataSetByOleDb(sql);
for (j = 0; j <= ds.Tables[0].Rows.Count - 1; j )
{
//try
//{
for (i = 0; i <= INN - 1; i )
{
dbnulltype = (object.ReferenceEquals(ds.Tables[0].Rows[j][i 4].ToString(), DBNull.Value) ? "DBNULL" : ds.Tables[0].Rows[j][i 4].ToString());
dbnulltype = dbnulltype.Trim();
//if ((string.IsNullOrEmpty(dbnulltype)) | (dbnulltype == "DBNULL"))
//{
// break; // TODO: might not be correct. Was : Exit For
//}
//dbnulltype = dbnulltype.Trim();
//if ((dbnulltype == "无"))
//{
// dbnulltype = "0";
//}
//if ((dbnulltype == "有"))
//{
// dbnulltype = "1";
//}
//if (dbnulltype.Substring(1, 1) == "<")
//{
// length = dbnulltype.Length;
// dbnulltype = dbnulltype.Substring(2, length - 1);
//}
Input[i] = Convert.ToDouble(dbnulltype);
}
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.ToString());
//}
if ((!string.IsNullOrEmpty(dbnulltype)) & (dbnulltype != "DBNULL"))
{
for (i = 0; i <= INN - 1; i )
{
if (Input[i] < (biaozhun[0, i] / 10.0))
{
Input[i] = biaozhun[0, i] / 10.0;
}
else if (Input[i] > biaozhun[1, i])
{
Input[i] = biaozhun[1, i];
}
Input[i] = Input[i] / (biaozhun[0, i] biaozhun[1, i]);
sr.Write(" ");
sr.Write(Input[i]);
}
}
}
sr.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.ToString());
}
}
private void startVC()
{
Thread t = new Thread(delegate()
{
System.Diagnostics.Process.Start("bp1.exe");
});
t.Start();
t.Join();
}
private void button2_Click(object sender, EventArgs e)
{
double temp = 0;
string sql1 = null;
string sql2 = null;
Int32 i = default(Int32);
Int32 j = default(Int32);
string dbnulltype = "";
string result = "";
string resultdeg = "";
//Dim times As Int32
sql1 = "delete * from 神经网络";
DataAccess.UpdateDataTable(sql1);
if ((string.IsNullOrEmpty(txtb_Wucha.Text.Trim()) | string.IsNullOrEmpty(txtb_shuchuyinghan.Text.Trim()) | string.IsNullOrEmpty(txtb_yinghanshuchu.Text.Trim()) | string.IsNullOrEmpty(txtb_studytimes.Text.Trim()) | string.IsNullOrEmpty(txtb_Yinggeshu.Text.Trim())))
{
MessageBox.Show("用户输入项中的值不能为空");
return;
}
System.IO.Directory.SetCurrentDirectory(Application.StartupPath);
if (StudyData_input(choiceitems) == false)
{
return;
}
Test_DataInput(choiceitems);
startVC();
MessageBox.Show("请稍后。。。");
Thread.Sleep(3000);
System.IO.FileStream fs = new System.IO.FileStream(Application.StartupPath "\\测试结果.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
txtb_shijixuexicishu.Text = sr.ReadLine();
txtb_shijiwucha.Text = sr.ReadLine();
fs.Close();
if ((Convert.ToDouble(txtb_shijiwucha.Text) > Convert.ToDouble(txtb_Wucha.Text) | Convert.ToInt32(txtb_studytimes.Text) < Convert.ToInt32(txtb_shijixuexicishu.Text)))
{
MessageBox.Show("在您设置的次数范围内没有达到指定的精度,请重新设置", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);//后面两个参数分别是 按钮类型和图标类型。
txtb_studytimes.Text = "8000";
txtb_Wucha.Text = "0.001";
txtb_Yinggeshu.Text = "40";
txtb_shuchuyinghan.Text = "0.5";
txtb_yinghanshuchu.Text = "0.5";
return;
}
System.IO.FileStream fas = new System.IO.FileStream(Application.StartupPath "\\测试结果.txt", FileMode.Open);
StreamReader sar = new StreamReader(fas, Encoding.Default);
txtb_shijixuexicishu.Text = sar.ReadLine();
txtb_shijiwucha.Text = sar.ReadLine();
string SechSql = " select 年份,点号,水期,含水层 ," choice " from water ";
DataSet ds = DataAccess.GetDataSetByOleDb(SechSql);
this.dataGridView1.DataSource = ds.Tables[0];
for (j = 0; j <= ds.Tables[0].Rows.Count - 1; j )
{
for (i = 0; i <= choiceitems - 1; i )
{
dbnulltype = (object.ReferenceEquals(ds.Tables[0].Rows[j][i 4].ToString(), DBNull.Value) ? "DBNULL" : ds.Tables[0].Rows[j][i 4].ToString());
dbnulltype = dbnulltype.Trim();
if ((string.IsNullOrEmpty(dbnulltype)) | (dbnulltype == "DBNULL"))
{
break; // TODO: might not be correct. Was : Exit For
}
}
if ((!string.IsNullOrEmpty(dbnulltype)) & (dbnulltype != "DBNULL"))
{
result = sar.ReadLine();
temp = Convert.ToDouble(result);
temp = temp * 5.0;
result = temp.ToString();
resultdeg = sar.ReadLine();
sql1 = "insert into 神经网络(年份,点号,含水层,水期,评价结果,水质等级)values(" "'" ds.Tables[0].Rows[j][0] "','" ds.Tables[0].Rows[j][1] "','" ds.Tables[0].Rows[j][3] "','" ds.Tables[0].Rows[j][2] "','" result "','" resultdeg "')";
DataAccess.UpdateDataTable(sql1);
}
}
sql2 = "select * from 神经网络";
ds = DataAccess.GetDataSetByOleDb(sql2);
this.dataGridView1.DataSource = ds.Tables[0];
fas.Close();
}
private static string ConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" Application.StartupPath "\\数据.mdb";//连接数据库字符串
private void Form1_Load(object sender, EventArgs e)
{
string SechSql = " select 年份,点号,水期,含水层," choice " from water";
DataSet ds = DataAccess.GetDataSetByOleDb(SechSql);
this.dataGridView1.DataSource = ds.Tables[0];
}
private void GroupBox2_Enter(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Reset();
openFileDialog1.FileName = "";
Text = openFileDialog1.Title;
openFileDialog1.Filter = "*.accdb|*.accdb";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//将路径显示出来
textBox1.Text = openFileDialog1.FileName;
filename = textBox1.Text;
FileStream hr = new FileStream(filename, FileMode.Open);
//按行文件读
StreamReader sh = new StreamReader(hr);
//每行存放的数据
string data;
while ((data = sh.ReadLine()) != null)
{
list.Add(data);
number ;
}
hr.Close();
sh.Close();
}
try
{
string path = Path.GetFileName(filename);//只获取文件名output.sub
//将选中的文件复制到debug目录下
string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(filename));
//bool isrewrite = true;
File.Copy(textBox1.Text, targetPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
标签: 神经网络
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论