string.Format使用方法

.Net技术 码拜 9年前 (2014-12-09) 1467次浏览 0个评论
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;
using System.Data.SqlClient;

namespace SqlCommandExample
{
public partial class Form1 : Form
{
string checkName = “radioButton1”;
string connectionString;
public Form1()
{
InitializeComponent();
connectionString = Properties.Settings.Default.MyDatabaseConnectionString;
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
RadioButton r = sender as RadioButton;
if (r.Checked) checkName = r.Name;
}
private void AddGrade()
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = “update MyTable2 set 成绩=成绩+10 where 姓名=’张三玉'”;
SqlCommand cmd = new SqlCommand(sql, conn);
listBox1.Items.Clear();
try
{
conn.Open();
int number = cmd.ExecuteNonQuery();
listBox1.Items.Add(string.Format(“修改了{0}条记录”, number));
}
catch(Exception ex)
{
listBox1.Items.Add(string.Format(“修改记录失败:{0}”, ex.Message));
}
}
}
private void ShowMyTable1()
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd=new SqlCommand (“select * from MyTable1”,conn );
listBox1.Items.Clear();
try
{
conn.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
listBox1.Items.Add(string.Format(“[{0}]{1}”, r[0], r[1]));
}
r.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, “读取记录失败!”);
}
}
}
private void CountMyTable2()
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
try
{
conn.Open();
cmd.CommandText = “select count (*) from MyTable2 where 姓名 like ‘王%'”;
int record = (int)cmd.ExecuteScalar();
cmd.CommandText = “select sum(成绩) form MyTable2 where 姓名 like ‘王%’ “;
double sumValue = Convert.ToDouble(cmd.ExecuteScalar());
MessageBox.Show( string.Format (“有[0]条姓王的记录,合计成绩为[1]”, record, sumValue));
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}

}
}
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
switch (checkName )
{
case “radioButton1”:
AddGrade();
break;
case “radioButton2″:
ShowMyTable1();
break;
case”radioButton3”:
CountMyTable2();
break;
}
this.Cursor = Cursors.Default;
}

}
}
第三个功能有点问题,帮我解答下吧,谢谢


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明string.Format使用方法
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!