C# 代码:
/// <summary>
/// 以字符串表示的SQLServer数据类型
/// </summary>
/// <param name="iXTypeVale"></param>
/// <returns></returns>
public static string GetSQLServerXTypeString(int iXTypeVale)
{
/*
34 image
35 text
36 uniqueidentifier
48 tinyint
52 smallint
56 int
58 smalldatetime
59 real
60 money
61 datetime
62 float
98 sql_variant
99 ntext
104 bit
106 decimal
108 numeric
122 smallmoney
127 bigint
165 varbinary
167 varchar
173 binary
175 char
189 timestamp
231 sysname
231 nvarchar
239 nchar
*/
switch (iXTypeVale)
{
case 34:
return "image";
case 35:
return "text";
case 36:
return "uniqueidentifier";
case 48:
return "tinyint";
case 52:
return "smallint";
case 56:
return "int";
case 58:
return "smalldatetime";
case 59:
return "real";
case 60:
return "money";
case 61:
return "datetime";
case 62:
return "float";
case 98:
return "sql_variant";
case 99:
return "ntext";
case 104:
return "bit";
case 106:
return "decimal";
case 108:
return "numeric";
case 122:
return "smallmoney";
case 127:
return "bigint";
case 165:
return "varbinary";
case 167:
return "varchar";
case 173:
return "binary";
case 175:
return "char";
case 189:
return "timestamp";
case 231:
return "nvarchar";
//case 231:
//SQL Server 实例包括用户定义的名为 sysname 的数据类型。
//sysname 用于表列、变量以及用于存储对象名的存储过程参数。sysname 的精确定义与标识符规则相关;
//因此,SQL Server 的各个实例会有所不同。sysname 与 nvarchar(128) 作用相同。
//return "sysname";
case 239:
return "nchar";
case 241:
return "xml";
}
return "未知";
}
C++ CLI(错的,永远返回NULL)
Type^ csharp_Field::SqlDbTypeConvertToCSharpType(SqlDbType sqlType)
{
switch (sqlType)
{
case SqlDbType::BigInt:
return System::Type::GetType("Int64");
case SqlDbType::Binary:
return System::Type::GetType("Object");
case SqlDbType::Bit:
return System::Type::GetType("Boolean");
case SqlDbType::Char:
return System::Type::GetType("String");
case SqlDbType::DateTime:
return System::Type::GetType("DateTime");
case SqlDbType::Decimal:
return System::Type::GetType("Decimal");
case SqlDbType::Float:
return System::Type::GetType("Double");
case SqlDbType::Image:
return System::Type::GetType("Object");
case SqlDbType::Int:
return System::Type::GetType("Int32");
case SqlDbType::Money:
return System::Type::GetType("Decimal");
case SqlDbType::NChar:
return System::Type::GetType("String");
case SqlDbType::NText:
return System::Type::GetType("String");
case SqlDbType::NVarChar:
return System::Type::GetType("String");
case SqlDbType::Real:
return System::Type::GetType("Single");
case SqlDbType::SmallDateTime:
return System::Type::GetType("DateTime");
case SqlDbType::SmallInt:
return System::Type::GetType("Int16");
case SqlDbType::SmallMoney:
return System::Type::GetType("Decimal");
case SqlDbType::Text:
return System::Type::GetType("String");
case SqlDbType::Timestamp:
return System::Type::GetType("Object");
case SqlDbType::TinyInt:
return System::Type::GetType("Byte");
case SqlDbType::Udt://自定义的数据类型
return System::Type::GetType("Object");
case SqlDbType::UniqueIdentifier:
return System::Type::GetType("Object");
case SqlDbType::VarBinary:
return System::Type::GetType("Object");
case SqlDbType::VarChar:
return System::Type::GetType("String");
case SqlDbType::Variant:
return System::Type::GetType("Object");
case SqlDbType::Xml:
return System::Type::GetType("Object");
default:
return null;
break;
}
}
C++ CLI(还是错的,永远返回NULL)
Type^ csharp_Field::SqlDbTypeConvertToCSharpType(SqlDbType sqlType)
{
switch (sqlType)
{
case SqlDbType::BigInt:
return System::Type::GetType("System::Int64");
case SqlDbType::Binary:
return System::Type::GetType("System::Object");
case SqlDbType::Bit:
return System::Type::GetType("System::Boolean");
case SqlDbType::Char:
return System::Type::GetType("System::String");
case SqlDbType::DateTime:
return System::Type::GetType("System::DateTime");
case SqlDbType::Decimal:
return System::Type::GetType("System::Decimal");
case SqlDbType::Float:
return System::Type::GetType("System::Double");
case SqlDbType::Image:
return System::Type::GetType("System::Object");
case SqlDbType::Int:
return System::Type::GetType("System::Int32");
case SqlDbType::Money:
return System::Type::GetType("System::Decimal");
case SqlDbType::NChar:
return System::Type::GetType("System::String");
case SqlDbType::NText:
return System::Type::GetType("System::String");
case SqlDbType::NVarChar:
return System::Type::GetType("System::String");
case SqlDbType::Real:
return System::Type::GetType("System::Single");
case SqlDbType::SmallDateTime:
return System::Type::GetType("System::DateTime");
case SqlDbType::SmallInt:
return System::Type::GetType("System::Int16");
case SqlDbType::SmallMoney:
return System::Type::GetType("System::Decimal");
case SqlDbType::Text:
return System::Type::GetType("System::String");
case SqlDbType::Timestamp:
return System::Type::GetType("System::Object");
case SqlDbType::TinyInt:
return System::Type::GetType("System::Byte");
case SqlDbType::Udt://自定义的数据类型
return System::Type::GetType("System::Object");
case SqlDbType::UniqueIdentifier:
return System::Type::GetType("System::Object");
case SqlDbType::VarBinary:
return System::Type::GetType("System::Object");
case SqlDbType::VarChar:
return System::Type::GetType("System::String");
case SqlDbType::Variant:
return System::Type::GetType("System::Object");
case SqlDbType::Xml:
return System::Type::GetType("System::Object");
default:
return null;
break;
}
}
正确的是:
Type^ csharp_Field::SqlDbTypeConvertToCSharpType(SqlDbType sqlType)
{
switch (sqlType)
{
case SqlDbType::BigInt:
return System::Type::GetType("System.Int64");
case SqlDbType::Binary:
return System::Type::GetType("System.Object");
case SqlDbType::Bit:
return System::Type::GetType("System.Boolean");
case SqlDbType::Char:
return System::Type::GetType("System.String");
case SqlDbType::DateTime:
return System::Type::GetType("System.DateTime");
case SqlDbType::Decimal:
return System::Type::GetType("System.Decimal");
case SqlDbType::Float:
return System::Type::GetType("System.Double");
case SqlDbType::Image:
return System::Type::GetType("System.Object");
case SqlDbType::Int:
return System::Type::GetType("System.Int32");
case SqlDbType::Money:
return System::Type::GetType("System.Decimal");
case SqlDbType::NChar:
return System::Type::GetType("System.String");
case SqlDbType::NText:
return System::Type::GetType("System.String");
case SqlDbType::NVarChar:
return System::Type::GetType("System.String");
case SqlDbType::Real:
return System::Type::GetType("System.Single");
case SqlDbType::SmallDateTime:
return System::Type::GetType("System.DateTime");
case SqlDbType::SmallInt:
return System::Type::GetType("System.Int16");
case SqlDbType::SmallMoney:
return System::Type::GetType("System.Decimal");
case SqlDbType::Text:
return System::Type::GetType("System.String");
case SqlDbType::Timestamp:
return System::Type::GetType("System.Object");
case SqlDbType::TinyInt:
return System::Type::GetType("System.Byte");
case SqlDbType::Udt://自定义的数据类型
return System::Type::GetType("System.Object");
case SqlDbType::UniqueIdentifier:
return System::Type::GetType("System.Object");
case SqlDbType::VarBinary:
return System::Type::GetType("System.Object");
case SqlDbType::VarChar:
return System::Type::GetType("System.String");
case SqlDbType::Variant:
return System::Type::GetType("System.Object");
case SqlDbType::Xml:
return System::Type::GetType("System.Object");
default:
return null;
break;
}
}
C#可以(所有功能在一个函数内完在,下面函数要是换成Lambda表达式更简洁):
/// <summary>
/// 显示信息
/// </summary>
/// <param name="sText"></param>
/// <param name="sCaption"></param>
/// <param name="bRtf"></param>
/// <param name="FontSize">信息文本</param>
/// <param name="sFontName"></param>
/// 创建时间:2019-12-29 最后一次修改时间:2022-02-25
public static string ShowDialogText(string sText, string sCaption = "",bool bRtf = false, int FontSize = 12,
string sFontName = "微软雅黑")
{
string sResult = "";
void RichTextBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
RichTextBox rtSernder = (RichTextBox)sender;
ContextMenu cm = new ContextMenu();
MenuItem miPaste = new MenuItem();
miPaste.Text = "粘贴";
void miPaste_Click(object miPasteSender, EventArgs miPasteE)
{
rtSernder.Paste();
}
miPaste.Click += miPaste_Click;
MenuItem miCopy = new MenuItem();
miCopy.Text = "复制";
miCopy.Enabled = rtSernder.SelectionLength != 0;
void MiCopy_Click(object miCopySender, EventArgs miCopyE)
{
rtSernder.Copy();
}
miCopy.Click += MiCopy_Click;
void MiBaidu_Click(object miBaiduSender, EventArgs miBaiduE)
{
if (rtSernder.SelectedText.Trim() != "")
{
LIEAuto.ie_baidu_query(rtSernder.SelectedText.Trim());
}
}
MenuItem miBaidu = new MenuItem();
miBaidu.Text = "百度";
miBaidu.Enabled = rtSernder.SelectionLength != 0;
miBaidu.Click += MiBaidu_Click;
void MiRead_Click(object MiReadSender, EventArgs MiReadE)
{
if (rtSernder.SelectedText.Trim() != "")
{
LSpeech.Speak(rtSernder.SelectedText);
}
}
MenuItem miRead = new MenuItem();
miRead.Text = "朗读";
miRead.Enabled = rtSernder.SelectionLength != 0;
miRead.Click += MiRead_Click;
cm.MenuItems.Add(miPaste);
cm.MenuItems.Add(miCopy);
cm.MenuItems.Add(miBaidu);
cm.MenuItems.Add(miRead);
rtSernder.ContextMenu = cm;
/*
int nOldStart = rtSernder.SelectionStart;
int nOldLength = rtSernder.SelectionLength;
rtSernder.LockPaint(true);
rtSernder.SelectAll();
rtSernder.ForeColor = RandomColor();
rtSernder.SelectionLength = 0;
rtSernder.SelectionStart = nOldStart;
rtSernder.SelectionLength = nOldLength;
rtSernder.LockPaint(false);
*/
}
}
void RichTextBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
RichTextBox sRTB = (RichTextBox)sender;
string sSelect = sRTB.SelectedText.Trim();
if (sSelect.Length != 0)
{
if (sCaption.IndexOf("五笔") != -1)
{
sRTB.Copy();
LIEAuto.ie_52wubi_query(sSelect);
}
else
{
//父控件已处理
}
}
}
Form f = new Form();
RichTextBox rtb = new RichTextBox();
rtb.Parent = f;
//rtb.BackColor = Color.Black;
//rtb.ForeColor = lg.RandomColor();
rtb.ForeColor = Color.DeepSkyBlue;
rtb.MouseDown += RichTextBox_MouseDown;
rtb.MouseDoubleClick += RichTextBox_MouseDoubleClick;
rtb.DetectUrls = true;
//rtb.ReadOnly = true;
if (bRtf)
rtb.Rtf = sText;
else
{
rtb.Text = sText;
rtb.SelectAll();
rtb.SelectionFont = new Font(sFontName, FontSize,FontStyle.Bold);
rtb.SelectionLength = 0;
}
rtb.Dock = DockStyle.Fill;
f.StartPosition = FormStartPosition.CenterScreen;
f.Height = 618;
f.Width = 1000;
//lf.WindowState = FormWindowState.Maximized;
void F_FormClosed(object fSender, FormClosedEventArgs fE)
{
sResult = rtb.Text;
}
f.FormClosed += F_FormClosed;
if (sCaption == "")
f.Text = "显示信息";
else
f.Text = sCaption;
try
{
f.ShowDialog();
}
finally
{
rtb.Dispose();
f.Dispose();
}
return sResult;
}
C++CLI变成这样:
ref class ShowDialogForm : Form
{
public:
RichTextBox^ rtb;
String^ sText;
String^ sCaption;
bool bRtf; int FontSize;
String^ sFontName;
String^ sResult;
public:
void miPaste_Click(System::Object^ miPasteSender, System::EventArgs^ miPasteE)
{
rtb->Paste();
}
void MiCopy_Click(System::Object^ miCopySender, System::EventArgs^ miCopyE)
{
rtb->Copy();
}
void MiBaidu_Click(System::Object^ miBaiduSender, System::EventArgs^ miBaiduE)
{
if (rtb->SelectedText->Trim() != "")
{
gce::ie_baidu_query(rtb->SelectedText->Trim());
}
}
void MiRead_Click(System::Object^ MiReadSender, System::EventArgs^ MiReadE)
{
if (rtb->SelectedText->Trim() != "")
{
gce::sp_Speak(rtb->SelectedText);
}
}
void RichTextBox_MouseDown(Object^ sender, MouseEventArgs^ e)
{
if (e->Button == Forms::MouseButtons::Right)
{
Forms::ContextMenu^ cm = gcnew Forms::ContextMenu();
MenuItem^ miPaste = gcnew MenuItem();
miPaste->Text = "粘贴";
miPaste->Click += gcnew System::EventHandler(this, &ShowDialogForm::miPaste_Click);
MenuItem^ miCopy = gcnew MenuItem();
miCopy->Text = "复制";
miCopy->Enabled = rtb->SelectionLength != 0;
miCopy->Click += gcnew System::EventHandler(this, &ShowDialogForm::MiCopy_Click);
MenuItem^ miBaidu = gcnew MenuItem();
miBaidu->Text = "百度";
miBaidu->Enabled = rtb->SelectionLength != 0;
miBaidu->Click += gcnew System::EventHandler(this, &ShowDialogForm::MiBaidu_Click);
MenuItem^ miRead = gcnew MenuItem();
miRead->Text = "朗读";
miRead->Enabled = rtb->SelectionLength != 0;
miRead->Click += gcnew System::EventHandler(this, &ShowDialogForm::MiRead_Click);
cm->MenuItems->Add(miPaste);
cm->MenuItems->Add(miCopy);
cm->MenuItems->Add(miBaidu);
cm->MenuItems->Add(miRead);
rtb->ContextMenu = cm;
//int nOldStart = rtSernder->SelectionStart;
//int nOldLength = rtSernder->SelectionLength;
//rtSernder->LockPaint(true);
//rtSernder->SelectAll();
//rtSernder->ForeColor = RandomColor();
//rtSernder->SelectionLength = 0;
//rtSernder->SelectionStart = nOldStart;
//rtSernder->SelectionLength = nOldLength;
//rtSernder->LockPaint(false);
}
}
void RichTextBox_MouseDoubleClick(Object^ sender, MouseEventArgs^ e)
{
String^ sSelect = rtb->SelectedText->Trim();
if (sSelect->Length != 0)
{
if (sCaption->IndexOf("五笔") != -1)
{
rtb->Copy();
gce::ie_52wubi_query(sSelect);
}
else
{
//父控件已处理
}
}
}
void F_FormClosed(Object^ fSender, FormClosedEventArgs^ fE)
{
sResult = rtb->Text;
}
ShowDialogForm(String^ sText, String^ sCaption, bool bRtf,
int FontSize, String^ sFontName)
{
this->sText = sText;
this->sCaption = sCaption;
this->bRtf = bRtf;
this->FontSize = FontSize;
this->sFontName = sFontName;
}
String^ ShowDialogText()
{
String^ sResult = "";
rtb = gcnew RichTextBox();
rtb->Parent = this;
//rtb->BackColor = Color->Black;
//rtb->ForeColor = lg->RandomColor();
rtb->ForeColor = Color::DeepSkyBlue;
rtb->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &ShowDialogForm::RichTextBox_MouseDown);
rtb->MouseDoubleClick += gcnew System::Windows::Forms::MouseEventHandler(this, &ShowDialogForm::RichTextBox_MouseDoubleClick);
rtb->DetectUrls = true;
//rtb->ReadOnly = true;
if (bRtf)
rtb->Rtf = sText;
else
{
rtb->Text = sText;
rtb->SelectAll();
rtb->SelectionFont = gcnew Drawing::Font(sFontName, FontSize, FontStyle::Bold);
rtb->SelectionLength = 0;
}
rtb->Dock = DockStyle::Fill;
StartPosition = FormStartPosition::CenterScreen;
Height = 618;
Width = 1000;
//lf->WindowState = FormWindowState->Maximized;
FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &ShowDialogForm::F_FormClosed);
if (sCaption == "")
Text = "显示信息";
else
Text = sCaption;
try
{
ShowDialog();
}
finally
{
Close();
}
return sResult;
}
};
String^ global_clr_extend::ShowDialogText(String^ sText, String^ sCaption, bool bRtf,
int FontSize, String^ sFontName)
{
ShowDialogForm^ f = gcnew ShowDialogForm(sText, sCaption, bRtf, FontSize, sFontName);
return f->ShowDialogText();
}
String^ global_clr_extend::ShowDialogText(String^ sText, String^ sCaption, bool bRtf, int FontSize)
{
return ShowDialogText(sText, sCaption, bRtf, FontSize, "微软雅黑");
}
String^ global_clr_extend::ShowDialogText(String^ sText, String^ sCaption, bool bRtf)
{
return ShowDialogText(sText, sCaption, bRtf, 12, "微软雅黑");
}
String^ global_clr_extend::ShowDialogText(String^ sText, String^ sCaption)
{
return ShowDialogText(sText, sCaption, false, 12, "微软雅黑");
}
String^ global_clr_extend::ShowDialogText(String^ sText)
{
return ShowDialogText(sText, "", false, 12, "微软雅黑");
}