转到正文

存档

标签: C#

It's common that we'll read data from xml files.

However, we may read xml string in our project. For example, you may like to use this feature if your project has using 'XML PATH' clause in SQL SERVER which will create a xml string. There's little document for examples of reading xml string data using C#. I'll provide one example will has been used in my project for Microsoft.

    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();

            string testStr = "url.Url 4/9/2010 11:20:33 PMurl.Url 4/10/2010 1:37:18 AMurl.Url 4/10/2010 1:57:39 AMurl.Url 4/10/2010 1:57:53 AMurl.Url 4/10/2010 1:59:15 AMurl.Url 4/11/2010 7:42:03 PM";

            doc.LoadXml(testStr);

            XmlNode root = doc.DocumentElement;
            IEnumerator ienum = root.GetEnumerator();
            XmlNode node;

            while (ienum.MoveNext())
            {
                node = (XmlNode)ienum.Current;
                Console.WriteLine(node.FirstChild.OuterXml);
                Console.WriteLine();
            }

        }
    }

It's very convenient to use this method with C# project. Easy and simple.
Good Luck!

Created by Knight

Error Description:

you may write a stored prcedure which return bigint value, the code can be :

SELECT CAST(SCOPE_IDENTITY() AS BIGINT)

The above statement returns the affected rows of tables and the return value is Big int.  In the .cs file, you may write code to get the return value of this stored procedure:

int id = (int)sqlCommand.ExecuteScalar();

I'm very sorry that the very strange error happen. We'll get the error: System.InvalidCastException: Specified cast is not valid. at....

Solution:

I don't know why this error happen. We can write code as follows to fix the erorr:

int id = (int)(long)sqlCommand.ExecuteScalar();

Why?

the sql command return an object, we firstly cast it into "long" type, and then cast it to "int" type, however, why can we cast it into "int" type directly? I'm so confused, Who can tell me the reasons?

Thank you very much in advanced.

1. malloc/free 与 new/delete的不同之处:

  • malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符。对于非内部数据类的对象而言,光用maloc/free 无法满足动态对象的要求。对象在创建的同时要自动执行构造函数, 对象消亡之前要自动执行析构函数。由于malloc/free 是库函数而不是运算符,不在编译器控制权限之内,不能够把执行构造函数和析构函数的任务强加malloc/free。而对于new和delete,编译器会帮我们完成大部分操作。
  • 我们在使用malloc开辟内存时,必须自己指定内存量,也就是说,必须使用sizeof等得到对象大小的操作符,如
    int *p = (int *) malloc(sizeof(int) * length);
    malloc 返回值的类型是void *,所以在调用malloc 时要显式地进行类型转换,将void * 转换成所需要的指针类型。malloc 函数本身并不识别要申请的内存是什么类型,它只关心内存的总字节数。
    而用new则无需用sizeof,也不用管变量类型,编译器会帮我们来搞定这个事情。
  • 重复的去free一段内存就会出错,而重复去delete一段对象则不会有事。

        继续阅读

Description:

As we all know, C++(standard) is good at efficiency but is poor at the GUI/CONFIG process. C# is good at GUI/CONFIG but poor at efficiency. The target of this project is to make C++ project as main project, and use C++ to call C# procedure. There are two projects in our project. The first one is C++(Adapter, caller) and the other is C#(Callee). C++ project is a adapter and used to be called by other C++ project. C# is called by adapter and used to do some configuration tasks.

Files List:

  • CsharpComCplusAdapter: 
               1.  CSharpComCPlusplus: a adapter can be called by your C++ project 
               2 . CsharpFront: a C# proejct which do some configuraion tasks 
               3. Lib: the adapter lib, if you want to use this adapter, you must include this lib                                       
               4. ConfigurationCPlusPlusAdapter.h: you must include file in your project. 
               5. CONNECTIONS: you must run rebuild.cmd if you modify the CsharpFront Code. (you must have administrator’s right to run this command)
  • MyApp.exe.config: you must place this file in your solution path. The content of this file can be ignored.
  • MyApp.exe.conifg.config: you must place this file in your solution path. You can config this file to fit your application.

How to use this project:

继续阅读

I spent one day to solve this problem. Oh, I'm so tired today :(
And I have some gained knowledge about this difficut procedure.

We cannot call C# procedure directly in our C++ program. C# is a .NET language, but C++(not CLI)  has nothing to do with .NET.
You can use C++/CLI to call C# becuase they are both in the same platform.  However, I don't want this result. I want use the standard C++ and we can call C# func directly in our program.

You can visit: http://www.codeproject.com/KB/cs/ManagedCOM.aspx to get important details.

There are some tricks:

  1. the interface in C# program must be public.
  2.  you must have "[assembly: ComVisible(true)]" in your AssemblyInfo.cs files (C# program)
  3. Open your C# project
      - Go to your project's properties (right-click menu)
      - Under the Build tab, make sure the "Register for COM interop" is checked (both for Debugging AND Release
  4. You can use VS GUID generator in Visual studio GUI tools.

继续阅读

Quickly development with C# and SQL server, ASP.NET~!

www.liaoqiqi.com网站PR查询 博客简洁版 博客Google_Site_Map 博客Baidu_Site_Map ?