菜鸟求助-课设-windows窗口应用程序连接mysql数据库问题-大神帮帮忙
如图我想做这么个登陆系统,但是不知道如何连接数据库,mysql的,贴上代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;
using System.Odbc;
using MySQLDriverCS;
namespace 登陆界面
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
这是program.cs的代码
namespace 登陆界面
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox用户名 = new System.Windows.Forms.TextBox();
this.textBox密码 = new System.Windows.Forms.TextBox();
this.button确定 = new System.Windows.Forms.Button();
this.button取消 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("SimSun", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(70, 42);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(412, 48);
this.label1.TabIndex = 0;
this.label1.Text = "客户关系管理系统";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(102, 102);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(60, 20);
this.label2.TabIndex = 1;
this.label2.Text = "用户名";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("SimSun", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.Location = new System.Drawing.Point(113, 163);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(49, 19);
this.label3.TabIndex = 2;
this.label3.Text = "密码";
//
// textBox用户名
//
this.textBox用户名.Location = new System.Drawing.Point(168, 102);
this.textBox用户名.Multiline = true;
this.textBox用户名.Name = "textBox用户名";
this.textBox用户名.Size = new System.Drawing.Size(216, 41);
this.textBox用户名.TabIndex = 3;
//
// textBox密码
//
this.textBox密码.Location = new System.Drawing.Point(168, 163);
this.textBox密码.Multiline = true;
this.textBox密码.Name = "textBox密码";
this.textBox密码.PasswordChar = '*';
this.textBox密码.Size = new System.Drawing.Size(216, 41);
this.textBox密码.TabIndex = 4;
this.textBox密码.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// button确定
//
this.button确定.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
this.button确定.Location = new System.Drawing.Point(168, 238);
this.button确定.Name = "button确定";
this.button确定.Size = new System.Drawing.Size(75, 23);
this.button确定.TabIndex = 5;
this.button确定.Text = "确定";
this.button确定.UseVisualStyleBackColor = true;
this.button确定.KeyDown += new System.Windows.Forms.KeyEventHandler(this.button确定_KeyDown);
//
// button取消
//
this.button取消.Location = new System.Drawing.Point(309, 238);
this.button取消.Name = "button取消";
this.button取消.Size = new System.Drawing.Size(75, 23);
this.button取消.TabIndex = 6;
this.button取消.Text = "取消";
this.button取消.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(552, 324);
this.Controls.Add(this.button取消);
this.Controls.Add(this.button确定);
this.Controls.Add(this.textBox密码);
this.Controls.Add(this.textBox用户名);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "登陆";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox用户名;
private System.Windows.Forms.TextBox textBox密码;
private System.Windows.Forms.Button button确定;
private System.Windows.Forms.Button button取消;
private void sqlupdate()
{
MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "账号", "root", "gyt57622257").AsString);
conn.Open();
MySQLCommand cmd = new MySQLCommand("insert into 账号 values(textBox用户名,textBox密码)", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
以上是form1.designer.cs的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 登陆界面
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button确定_KeyDown(object sender, KeyEventArgs e)
{
}
private void button确定_Click(object sender, EventArgs e)
{
}
}
}
以上是form1.cs的代码,就是点击“确定”之后的事件代码
我就是搞不懂
1.那个数据连接的函数该放在哪个代码里?
2.怎么让他点击确定然后就调用那个函数来连接数据库,执行验证连接等
因为他们都是独立文件所以不知道怎么搞,还请大神指点啊 --------------------编程问答-------------------- 调用mysql和其他数据库有区别么?
你自己写个操作类,或是直接在.cs类文件中调用应该都是没问题的吧? --------------------编程问答-------------------- 说的详细点。我很菜的,大致说下步骤,求你了 呵呵 --------------------编程问答-------------------- 代码放到确定按钮里
MySQLCommand cmd = new MySQLCommand("insert into 账号 values(textBox用户名,textBox密码)", conn)
您这个是个插入记录的语句。
你要验证,必须得从数据库中抽数据。关键是,判断输入的 用户and密码 在数据库里时候有记录。
你抽、插搞混了
--------------------编程问答--------------------
上边图片挂了,来个 --------------------编程问答-------------------- 哦,我知道sql语句错了,但是把数据库语句放在确定里边,提示有不认识的字符还是什么的,这问题对大神们肯定不是问题了,帮忙搞一搞,我就是反不过劲来,这东西应该是一通百通的把,帮帮忙,写一下....小弟在此跪谢了 --------------------编程问答--------------------
补充:.NET技术 , C#